Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Grails Controller Unit Test to verify layout and template specified in render call
<%-- located in views/layouts/ajax.gsp --%>
<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'
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