开发者

Access model and view information in grails controller integration test

Is there any way I can get what view and what ha开发者_JS百科s been passed as model after issuing render command in controller in integration test? Eg.:

render view: 'edit', model: [profileInstance: someProfile]

Any chance I can retrieve model and view to perform some assertions? I know how to do it in unit tests, but it would be handy in integration tests as well.


You can change the render method from the Controller to be able to get the map that is passed to it in order to be able to get the model and the view. For this, you can declare a variable in the test method called renderMap and then change the render method to set it like this:

Map renderMap

registerMetaClass(MyController.class)
MyController.metaClass.render = {Map m ->
  renderMap = m
}
...
// Instantiate controller
// Call controller action

Then you can access some of the objects you set in the model like this:

def someObject = renderMap.model.someObject

And the view like this:

String view = renderMap.view

At some point you might want to restore your controller's metaClass as it was.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜