是否有可能获得 Spock 中的呼叫号码
Is it possible to get the number of call in Spock
是否可以获取Spock中模拟方法的调用次数?我想测试该方法是否被调用了特定次数,但每秒返回的值应该不同。下面的伪代码应该更具体地说明我想要什么:
void "My idea of a test" {
when:
...
then:
10 * someService(_) >> {
return theNumberOfTheCall % 2 ? SOME_VALUE : null // theNumberOfTheCall should illustrate my purpose
}
// so the service will return [null, SOME_VALUE, null, SOME_VALUE, null ...]
}
Spock 本身不会将多个调用传递给模拟方法,但您可以使用 AtomicInteger
来增加测试方法中定义的计数器。考虑以下简单示例:
import spock.lang.Specification
import java.util.concurrent.atomic.AtomicInteger
class InvocationCounterSpec extends Specification {
def "should return different value depending on invocation counter"() {
setup:
final AtomicInteger counter = new AtomicInteger(0)
final SomeService someService = Mock(SomeService)
final SomeClass someClass = new SomeClass(someService)
when:
someClass.run()
then:
10 * someService.someMethod() >> {
return counter.getAndIncrement() % 2 ? "SOME_VALUE" : null
}
}
static interface SomeService {
def someMethod()
}
static class SomeClass {
private final SomeService someService
SomeClass(SomeService someService) {
this.someService = someService
}
void run() {
(0..<10).each {
def value = someService.someMethod()
println "someService.someMethod() returned ${value}"
}
}
}
}
在此示例中,someClass.run()
方法调用模拟 someService.someMethod()
10 次。我们使用一个为我们计算调用次数的计数器存根 someService.someMethod()
返回值。如果您 运行 此测试,您将看到以下输出:
someService.someMethod() returned null
someService.someMethod() returned SOME_VALUE
someService.someMethod() returned null
someService.someMethod() returned SOME_VALUE
someService.someMethod() returned null
someService.someMethod() returned SOME_VALUE
someService.someMethod() returned null
someService.someMethod() returned SOME_VALUE
someService.someMethod() returned null
someService.someMethod() returned SOME_VALUE
希望对您有所帮助。
可以使用链接来完成:
10 * someService(_) >>> (1..10).collect {
it % 2 ? SOME_VALUE : null
}
是否可以获取Spock中模拟方法的调用次数?我想测试该方法是否被调用了特定次数,但每秒返回的值应该不同。下面的伪代码应该更具体地说明我想要什么:
void "My idea of a test" {
when:
...
then:
10 * someService(_) >> {
return theNumberOfTheCall % 2 ? SOME_VALUE : null // theNumberOfTheCall should illustrate my purpose
}
// so the service will return [null, SOME_VALUE, null, SOME_VALUE, null ...]
}
Spock 本身不会将多个调用传递给模拟方法,但您可以使用 AtomicInteger
来增加测试方法中定义的计数器。考虑以下简单示例:
import spock.lang.Specification
import java.util.concurrent.atomic.AtomicInteger
class InvocationCounterSpec extends Specification {
def "should return different value depending on invocation counter"() {
setup:
final AtomicInteger counter = new AtomicInteger(0)
final SomeService someService = Mock(SomeService)
final SomeClass someClass = new SomeClass(someService)
when:
someClass.run()
then:
10 * someService.someMethod() >> {
return counter.getAndIncrement() % 2 ? "SOME_VALUE" : null
}
}
static interface SomeService {
def someMethod()
}
static class SomeClass {
private final SomeService someService
SomeClass(SomeService someService) {
this.someService = someService
}
void run() {
(0..<10).each {
def value = someService.someMethod()
println "someService.someMethod() returned ${value}"
}
}
}
}
在此示例中,someClass.run()
方法调用模拟 someService.someMethod()
10 次。我们使用一个为我们计算调用次数的计数器存根 someService.someMethod()
返回值。如果您 运行 此测试,您将看到以下输出:
someService.someMethod() returned null
someService.someMethod() returned SOME_VALUE
someService.someMethod() returned null
someService.someMethod() returned SOME_VALUE
someService.someMethod() returned null
someService.someMethod() returned SOME_VALUE
someService.someMethod() returned null
someService.someMethod() returned SOME_VALUE
someService.someMethod() returned null
someService.someMethod() returned SOME_VALUE
希望对您有所帮助。
可以使用链接来完成:
10 * someService(_) >>> (1..10).collect {
it % 2 ? SOME_VALUE : null
}