【发布时间】:2011-05-24 15:00:27
【问题描述】:
对于集成测试,我想有一个 .save() 故意以测试符合 else-condition。
我的测试班是这样做的:
来自 UserService.groovy:
User user = User.findByXyz(xyz)
if (user) {
// foo
if (user.save()) {
// bar
} else {
// I WANT TO GET HERE
}
}
到目前为止我尝试过的方法都失败了:
我在 UserServiceTests.groovy 中尝试过的内容:
def uControl = mockFor(User)
uControl.demand.save { flush -> null } // in order to test a failing user.save()
def enabledUser = userService.enableUser(u.confirmationToken)
uControl.verify()
// or the following:
User.metaClass.'static'.save = { flush -> null } // fails *all* other tests too
如何正确地从集成测试中进入 else 块?
【问题讨论】:
-
更改用户的元类应该可以工作,您只需要先调用 registerMetaClass 以便在测试完成后恢复元类,如果您有,请不要忘记调用 super.tearDown()在你的课堂上实现它。
标签: testing grails groovy mocking integration-testing