【问题标题】:Spec2: Getting second parameter argument passed into the mocked method invocationSpec2:获取传递给模拟方法调用的第二个参数参数
【发布时间】:2014-10-13 19:50:40
【问题描述】:

我有一个接受 3 个参数的模拟服务。如何访问第二个参数?

mockedService.invoke(arg1, arg2, arg3) answers {
  (params, mock) => {
     //Do something with params.arg2 to the value that is returned from the invocation
  }
}

在文档中,他们声明“方法参数的数组将被传递”我如何访问第二个参数(在这种情况下为arg2)?我是否将paramsList[Any] 一起投射?

谢谢

【问题讨论】:

    标签: scala unit-testing mockito matcher specs2


    【解决方案1】:

    您需要将参数与Array 匹配,如下所示:

    import org.specs2.Specification
    import org.specs2.mock.Mockito
    
    class TestSpec extends Specification with Mockito { def is = s2"""
      test $e1
    """
    
      def e1 = {
        val mockedService = mock[Service]
        mockedService.invoke(1, 2, 3).answers { (params, mock) =>
          params match {
            case Array(a, b: Int, c) => b + 2
          }
        }
        mockedService.invoke(1, 2, 3) must_== 4
      }
    }
    
    trait Service {
      def invoke(arg1: Int, arg2: Int, arg3: Int) = 1
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 2012-12-12
      • 2018-07-24
      相关资源
      最近更新 更多