【问题标题】:Mock multiple calls to the same method in spock在 spock 中模拟对同一方法的多次调用
【发布时间】:2020-11-13 07:53:49
【问题描述】:

我目前正在为一个 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"
   }
}

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

【问题讨论】:

    标签: groovy spock


    【解决方案1】:

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

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

    那么getDateBasedOnValue()将第一次返回"some date string 1",第二次返回"some date string 2"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 2020-02-17
      • 2016-03-04
      • 2013-09-12
      • 2019-08-29
      相关资源
      最近更新 更多