使用指定的超时验证Spock mock

Jak*_*ski 7 testing unit-testing mocking spock

在Mockito中,可以选择验证是否已调用mock方法,并为此验证指定超时(VerificationWithTimeout),例如:

verify(mock, timeout(200).atLeastOnce()).baz();
Run Code Online (Sandbox Code Playgroud)

在Spock中有没有相当于这样的功能?

th3*_*org 8

我试图使用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内设置,则测试将因超时异常而失败.


Pet*_*ser 7

Spock没有相应的东西.(PollingConditions只能用于条件,而不能用于交互.)最接近的是sleep()then块中添加一个语句:

when:
...

then:
sleep(200)
(1.._) * mock.baz()
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这样的推测 - 没有看过代码,但已经知道它是错的...... (4认同)
  • 不是现在.这个功能对我来说听起来很臭,我试图抵制实施臭嘲弄的功能. (2认同)
  • 气味是使用模拟进行集成测试. (2认同)