我在从其他人那里继承的控制器的单元测试中看到以下代码:
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
你告诉 Spock,有问题的方法必须只调用一次(1 * controller.cService.getRegions() >> []
的意思是,这个服务的getRegions
必须调用一次(1 *
(,并将返回一个空列表 ( >> []
((。 但事实并非如此。 这就是错误消息告诉您的内容(0 invocations
(。
请查看以下示例:-
def "List Invocation Calls Test"() {
given:
List list = Mock();
when:
list.add(5)
list.add(15)
list.add(25)
then:
3 * list.add(_)
}