Skip to content

Instantly share code, notes, and snippets.

@jeffsheets
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffsheets/8950251 to your computer and use it in GitHub Desktop.
Save jeffsheets/8950251 to your computer and use it in GitHub Desktop.
Grails Controller Unit Test to verify layout and template specified in render call
<%-- located in views/layouts/ajax.gsp --%>
<g:layoutBody/>
<r:layoutResources disposition="defer"/>
/* action that renders a template back to browser,
* and uses custom simple 'ajax' layout */
def ajaxResults() {
def results = workService.querySomeWork()
render (template: "ajaxResults", model:[results:results as JSON], layout:'ajax')
}
void "ajaxResults action renders correct template and layout"() {
given: 'template is mocked out'
views['/ajaxlayout/_ajaxResults.gsp'] = '${results}'
when: 'ajaxResults url is hit'
controller.ajaxResults()
then: 'proper template and layout are rendered'
1 * workService.querySomeWork() >> [[name:'jeff',count:23],[name:'scott',count:12]]
//Tests that results were injected as json into ajaxResults template
response.contentAsString == '[{"name":"jeff","count":23},{"name":"scott","count":12}]'
//Tests that sitemesh layout of 'ajax' was specified in render attribute
request[GrailsLayoutDecoratorMapper.LAYOUT_ATTRIBUTE] == 'ajax'
response.redirectedUrl == null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment