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

Groovy 在spock中模拟对同一方法的多个调用,groovy,spock,Groovy,Spock,我目前正在为groovy应用程序编写单元测试用例 class StorePage{ .. .. str1 = obj.getDateBasedOnValue("A"); str2 = obj.getDateBasedOnValue("B"); } 测试班 classStorePageSpec extends Specification{ Obj obj = Mock(Obj) ... def "test

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

class StorePage{
   ..
   ..
   str1 = obj.getDateBasedOnValue("A");
   str2 = obj.getDateBasedOnValue("B");
}
测试班

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

有人能告诉我这是不是模仿斯波克两个电话的正确方法吗?如果没有,请指导我找到正确的解决方案。

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

def "testCase 1"(){
    obj.getDateBasedOnValue(_) >>> ["some date string 1", "some date string 2"]
}
然后
getDateBasedOnValue()
将第一次返回
“某个日期字符串1”
,第二次返回
“某个日期字符串2”