在 spock 中模拟对同一方法的多次调用

Sup*_*sic 3 groovy spock

我目前正在为一个 groovy 应用程序编写单元测试用例

class StorePage{
   ..
   ..
   str1 = obj.getDateBasedOnValue("A");
   str2 = obj.getDateBasedOnValue("B");
}
Run Code Online (Sandbox Code Playgroud)

测试班

classStorePageSpec extends Specification{
   Obj obj = Mock(Obj)
   ...
   def "testCase 1"(){
      obj.getDateBasedOnValue(_) >> "some date string 1"
      obj.getDateBasedOnValue(_) >> "some date string 2"
   }
}
Run Code Online (Sandbox Code Playgroud)

有人能告诉我这是否是在 spock 中模拟这两个调用的正确方法吗?如果没有,请指导我找到正确的解决方案。

cgr*_*rim 5

要在连续调用时返回不同的值,请使用三重右移 (>>>) 运算符:

def "testCase 1"(){
    obj.getDateBasedOnValue(_) >>> ["some date string 1", "some date string 2"]
}
Run Code Online (Sandbox Code Playgroud)

然后getDateBasedOnValue()"some date string 1"第一次和"some date string 2"第二次返回。