我想编写参数化测试,使用 where:
来验证方法 x
是否被调用。我想将 x
作为参数传递。像
when:
someService.request(input)
then:
1 * (closure.call(otherService))
where
input | closure
1 | {OtherService service -> service.method1(2, 3)}
2 | {OtherService service -> service.method2(4, 5, 6)}
但是我得到一个错误:
Too few invocations for:
1 * (closure.call(otherService)) (0 invocations)
Unmatched invocations (ordered by similarity):
1 * otherService.deleteUserMessage(2,3)
我想做的事情可行吗?
最佳答案
通过使用基于交互的测试可以轻松完成您想要实现的目标,而不是试图使测试过程复杂化并使其难以理解:
when:
someService.request(1)
then:
1 * service.method1(2, 3)
when:
someService.request(2)
then:
1 * service.method1(4, 5, 6)
您是否期待比上述两者更多的互动?
关于testing - spock:使用闭包作为参数验证交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36985699/