在我的应用程序中,AddressBookController
有一个editContact
操作,如下所示:
def editContact(){
AddressBook addressBook=AddressBook.findById(params.addressBookId)
render view: 'addContact', model: [addressBook:addressBook]
}
现在我正在尝试将视图和模型测试为:
class AddressBookControllerSpec extends IntegrationSpec{
AddressBookController addressBookController = new AddressBookController()
void setup() {
}
def "editContact action renders view with addressBook details"() {
when:
addressBookController.editContact()
then:
addressBookController.modelAndView.model.addressBook
}
void cleaup() {
// Tear down logic here
}
}
但是我的测试用例因堆栈跟踪而失败:
运行2次spock测试... 1 of 2
| Failure: editContact action renders view with addressBook details(cvsurgeon.AddressBookControllerSpec)
| java.lang.NullPointerException: Cannot get property 'model' on null object
at cvsurgeon.AddressBookControllerSpec.setup(AddressBookControllerSpec.groovy:15)
| Completed 2 spock tests, 1 failed in 660ms
| Tests FAILED - view reports in ....
可能是什么问题。
答案 0 :(得分:1)
试试这个..,。
AddressBookController controller = new AddressBookController()
def "editContact action renders view with addressBook details"() {
when:
controller.params.addressBookId = 1 //valid id
controller.editContact()
then:
assert controller.modelAndView.model.addressBook
assert controller.modelAndView.modelMap.addressBook
assert controller.modelAndView.viewName == "/addressBook/addContact"
}