【发布时间】:2018-08-02 09:26:46
【问题描述】:
问题很奇怪,因为几乎相同的操作返回了正确的模型,不同之处在于我不是渲染模板而是视图。
首先是 controller 动作:
如您所见,渲染的模型在这里存在
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
}
}
如果我将 'template' 与 'view' 交换,则测试通过
服务:
Author save(Author author) {
return author?.save(flush: true)
}
测试本身:
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
}
错误:
Condition not satisfied:
model.author
| |
| null
[authorInstance:daily.journal.Author : (unsaved)]
我知道您可以检查渲染模板的内容,但我明确地针对模型。有没有机会测试一下?
【问题讨论】: