相关疑难解决方法(0)

如何在Scala中使用函数参数模拟方法?

我正在尝试模拟一个带有call-by-name参数的方法调用:

import org.scalatest.WordSpec
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

trait Collaborator {
   def doSomething(t: => Thing)
}

trait Thing

@RunWith(classOf[JUnitRunner])
class Test extends WordSpec with MockitoSugar {
   "The subject under test" should {
      "call the collaborator" in {
         // setup
         val m = mock[Collaborator]
         val t = mock[Thing]

         // test code: this would actually be invoked by the SUT
         m.doSomething(t)

         // verify the call
         verify(m).doSomething(t)
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

我主要对Mockito感兴趣,因为我正在使用它,但我有兴趣看看是否有任何主要的模拟框架能够进行这种测试.测试在运行时在线上失败,verify出现类似的错误

Argument(s) are different! Wanted:  
collaborator.doSomething(  
   ($anonfun$apply$3) <function> …
Run Code Online (Sandbox Code Playgroud)

scala mocking

20
推荐指数
1
解决办法
1万
查看次数

标签 统计

mocking ×1

scala ×1