在 Mockito 中,有一个选项可以验证是否已调用 mock 方法,并指定此验证的超时时间 (VerificationWithTimeout
),例如:
verify(mock, timeout(200).atLeastOnce()).baz();
在 Spock 中是否有任何等效的功能?
最佳答案
我试图使用 PollingConditions 来满足类似的场景(这没有帮助),但我却在 Spock 的 BlockingVariables 中找到了满足感。 .验证 SomeService.method() 在给定的超时期限内至少在函数 ClassBeingTested.method() 中被调用一次:
def "verify an interaction happened at least once within 200ms"(){
given:
def result = new BlockingVariable<Boolean>(0.2) // 200ms
SomeService someServiceMock = Mock()
someServiceMock.method() >> {
result.set(true)
}
ClassBeingTested clazz = new ClassBeingTested(someService: someServiceMock)
when:
clazz.someMethod()
then:
result.get()
}
设置结果后,将满足阻塞条件,并且 result.get() 必须返回 true 才能使条件通过。如果在200ms内没有设置成功,则测试失败,超时异常。
关于unit-testing - 使用指定的超时验证 Spock 模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21666874/