根据spock中的调用次数嘲笑返回一个方法 [英] mock out return of a method base on the number of invocation only in spock
本文介绍了根据spock中的调用次数嘲笑返回一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以基于第n次调用spock中的方法来模拟返回值?请注意,我不想指定传入的参数,因为它对于特定的测试用例无关紧要。
例如,对于第一个调用,它应该返回x ,第二次调用应该返回y。
$ b someObject.someMethod(* _)>>> ['x','y']
它会返回 x 第一次调用时为code>,第二次调用该方法时为
y
。
示例:
voidtest something(){
given:
def sample = Mock(Sample){
someMethod(_)>>> ['Hello','World']
}
expect:
sample.someMethod('foo')=='Hello'
sample.someMethod(' bar')=='World'
}
class示例{
def someMethod(def a){
return a
}
}
Is it possible to mock out the return value of a method in spock based on the nth time it was called? Note that I don't want to specify the parameters passed in because it does not matter for a specific test case.
For example, for the first call it should return x, for the second call it should return y.
解决方案
Yes it is possible.
someObject.someMethod(*_) >>> [ 'x', 'y' ]
It will return x
on first invocation and y
on second invocation of the method.
Example:
void "test something"() {
given:
def sample = Mock(Sample){
someMethod(_) >>> [ 'Hello', 'World' ]
}
expect:
sample.someMethod( 'foo' ) == 'Hello'
sample.someMethod( 'bar' ) == 'World'
}
class Sample {
def someMethod(def a) {
return a
}
}
这篇关于根据spock中的调用次数嘲笑返回一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文