The problem is quite odd, since almost the same action returns the correct model with the difference that I'm not rendering a template but view.
First the controller action:
The rendered model exist here as you can see
def saveAjax(Author authorInstance){
if(!authorInstance.validate()){
respond view: 'index', authorInstance.errors
} else {
def author = new Author(username: authorInstance.username)
authorService.save(author)
render(template: 'author_row', model:[author:author]) //<--here the model is correct
}
}
If I swap 'template' with 'view', the test passes
The service:
Author save(Author author) {
return author?.save(flush: true)
}
The test itself:
void "Test the saveAjax action"() {
when:"you do not have the correct params"
controller.saveAjax(new Author())
then:"expect to be redirected to index"
view == 'index'
when:"you have the correct params"
response.reset()
controller.saveAjax(new Author(username: "Alice"))
then:"expect to have model author"
model.author //<-- here I get author = null
}
The error:
Condition not satisfied:
model.author
| |
| null
[authorInstance:daily.journal.Author : (unsaved)]
I know that you could check the content of the rendered template but I am explicitly aiming for the model. Is there any chance of testing it?
Turns out that I hit upon a Grails convention over configuration thingy. Despite that the model is named author, it could be tested only as authorInstance. So I changed it to:
then:"expect to have model author"
model.authorInstance
in the spec and...it passed. Still I will be glad if someone could explain to me why?