我尝试使用单元测试来验证在promise中调用方法。
我有一个CallerService,它将在任务中调用classA.methodA(),
class CallerService {
def classA
def callA() {
Promise p = task {
classA.methodA()
}
p.onComplete {
println "complete"
}
p.onError { Throwable err ->
println "there was an error"
}
}
}
和一个模拟ClassA的单元测试,并尝试验证methodA被调用一次。
def "calling A"() {
given:"a mock classA"
def mockClassA = Mock(ClassA)
service.classA = mockClassA
when:"callA is called"
service.callA()
then:"methodA should be called once"
1* mockClassA.methodA()
}
测试失败,因为模拟被调用了两次。
| Failure: calling A(promisespike.CallerServiceSpec)
| Too many invocations for:
1* mockClassA.methodA() (2 invocations)
Matching invocations (ordered by last occurrence):
2 * mockClassA.methodA() <-- this triggered the error
这是我应该期待的结果还是我错误地设置了我的测试?