Last active
August 29, 2015 13:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%-- located in views/layouts/ajax.gsp --%> | |
<g:layoutBody/> | |
<r:layoutResources disposition="defer"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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