我有一個控制器方法需要返回一個JSON映射,其中的一個項目是模板中的html。我正在使用的代碼,如在控制器中使用g.applyLayout中斷單元測試
map['html'] = g.applyLayout(name: 'layoutName', g.render(template: 'template', model: [...])
...
render(map as JSON)
而我在控制器單元測試調用此方法。然而,當我運行測試,我得到以下異常:
java.lang.IllegalStateException: Cannot return Sitemesh factory it has not been set!
at org.springframework.util.Assert.state(Assert.java:384)
at org.codehaus.groovy.grails.web.sitemesh.FactoryHolder.getFactory(FactoryHolder.java:39)
at org.codehaus.groovy.grails.web.sitemesh.FactoryHolder$getFactory.call(Unknown Source)
...
如果我拿出g.applyLayout(),只是使用g.render()的測試運行。我究竟做錯了什麼?
更新
這裏是我的單元測試類
@TestFor(ContactsController)
@Mock([Contact, User, Company])
class ContactsControllerTests {
@Test
void testSaveContact() {
defineBeans {
contactsManagerService(ContactsManagerService)
}
Company company = new Company(name: 'COMPANY 1')
company.save(validate: false)
Contact userContact = new Contact(name: 'user contact', email: '[email protected]')
Contact companyContact = new Contact(name: 'company contact', email: '[email protected]')
userContact.save(validate: false)
companyContact.save(validate: false)
new User(name: 'user 1', password: 'foo', contact: userContact, company: company).save(validate: false, deepValidate: false)
controller.params.name = ''
controller.params.email = '[email protected]'
controller.saveContact(userContact.id)
assertNotNull(response.json.errors) // Name cannot be empty
response.reset()
controller.params.name = 'Updated Name'
controller.params.email = '[email protected]'
controller.saveContact(userContact.id)
assertTrue(response.json.success)
Contact contact = Contact.read(userContact.id)
assertEquals('Updated Name', contact.name)
assertEquals('[email protected]', contact.email)
}
}
你能分享你的完整單元測試課嗎? –
更新了問題以包含單元測試。 – BillGoneMad