【发布时间】:2015-03-04 21:25:29
【问题描述】:
我在单元测试中看到我从其他人那里继承的控制器的以下代码:
when: "When the controller executes a registration"
controller.index()
then: "the signup should show registration again"
1 * controller.cService.getRegions() >> []
1 * controller.dService.chkAvail(_) >> "AVAILABLE"
1 * controller.uService.createUser(_) >> { a-> throw new RuntimeException("Roll me back!")}
1 * controller.pService.registerPayMethod(_) >> { cc-> true }
view == "/signUp/su"
我了解 spock 单元测试的基础知识,但我不了解这些 1 * 行。
我也遇到了多个错误,例如:
junit.framework.AssertionFailedError: Too few invocations for:
1 * controller.cService.getRegions() >> [] (0 invocations)
Unmatched invocations (ordered by similarity):
None
【问题讨论】:
-
错误说,该方法没有被调用,而您希望它调用一次
-
@cfrick 谢谢,当开发人员没有阅读基础知识时会发生这种情况。因此,通过这种方式,我们可以验证特定方法是否被调用了一定次数。如果你想写你的评论作为答案。
标签: unit-testing grails spock