Jak*_*ski 7 testing unit-testing mocking spock
在Mockito中,可以选择验证是否已调用mock方法,并为此验证指定超时(VerificationWithTimeout),例如:
verify(mock, timeout(200).atLeastOnce()).baz();
Run Code Online (Sandbox Code Playgroud)
在Spock中有没有相当于这样的功能?
我试图使用PollingConditions来满足类似的场景(这没有帮助),但是在Spock的BlockingVariables中找到了满足感.要验证在给定的超时时间内函数ClassBeingTested.method()中至少调用一次SomeService.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()
}
Run Code Online (Sandbox Code Playgroud)
设置结果后,将满足阻塞条件,并且result.get()必须为要传递的条件返回true.如果无法在200ms内设置,则测试将因超时异常而失败.
Spock没有相应的东西.(PollingConditions只能用于条件,而不能用于交互.)最接近的是sleep()在then块中添加一个语句:
when:
...
then:
sleep(200)
(1.._) * mock.baz()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4612 次 |
| 最近记录: |