Using Grails 2.1.0
It seems that doing this from a controller:
render(view: "someView", model: [modelEntry: "hello"])
allows me to do this in a unit test for that controller:
controller.method()
assert model.modelEntry == "hello"
However, if I change the controller to do this:
render(template: "someTemplate", model: [modelEntry: "hello"])
Now the model instance in the test is an empty array. I've done quite a bit of searching about this, and most of the solutions seem to be for Grails 1, often involving the modelAndView
object (which doesn't exist in my test) or renderArgs
(ditto).
The only solution I've found is to manually override the views within the test, like this:
views['_someTemplate.gsp'] = '${modelEntry}'
and then making assertions about the string. But I dislike this solution because it:
- requires the test knows the filename of the template
- makes it difficult to test model entries that don't have good toString() methods
- makes it difficult to make multiple assertions about related model entries.
Is there any way to more directly get at the entries in the model from a test case when the controller renders a template?