0
我有一個PersonController的測試,它只是執行沒有參數的save(),所以創建一個無效的人。它應該返回模型中的無效人員並顯示創建視圖。但是模型是空的。Grails 2.4.4控制器測試保存()返回空模型
測試:
import org.junit.*
import grails.test.mixin.*
@TestFor(PersonController)
@Mock(Person)
class PersonControllerTests {
...
void testSave() {
controller.save() // creates invalid person, redirects to create
assert model.personInstance != null
assert view == '/person/create'
response.reset()
populateValidParams(params)
controller.save()
assert response.redirectedUrl == '/person/show/1'
assert controller.flash.message != null
assert Person.count() == 1
}
...
}
控制器:
class PersonController {
...
def save() {
def personInstance = new Person(params)
if (!personInstance.save(flush: true)) {
render(view: "create", model: [personInstance: personInstance])
return
}
flash.message = message(code: 'default.created.message', args: [message(code: 'person.label', default: 'Person'), personInstance.id])
redirect(action: "show", id: personInstance.id)
}
...
}
輸出:
junit.framework.AssertionFailedError: Assertion failed:
assert model.personInstance != null
| | |
[:] null false
at demographic.PersonControllerTests.testSave(PersonControllerTests.groovy:43)
我怎樣才能得到正確的模型?
這是預期的行爲還是這是一個Grails錯誤?
我不能肯定這是你的問題,並沒有看到PersonController'的'完整定義,但你嘗試過設置在測試'controller.request.method =「POST''(前'控制器.save()')? –
我能夠通過跟蹤你在github上的代碼來檢查這一點:-) –