我正在将Scalamock与ScalaTest一起使用,并试图模拟Java接口。我目前有:
private val _iface = mock [MyInterface]
现在我想做
_iface expects `someMethod returning "foo" once
但是编译器没有找到expects
。
我导入了org.scalatest._
和org.scalamock.scalatest._
。我还缺少什么?
首先,ScalaMock 3不太支持代理mock,我认为它们将在ScalaMock 4中完全删除。你真的需要使用代理模拟而不是宏模拟吗?
这应该有效:
package example
import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalamock.scalatest.proxy.MockFactory
trait MyInterface {
def someMethod : String
}
class MyTest extends FlatSpec with Matchers with MockFactory {
"MyInterface" should "work" in {
val m = mock[MyInterface]
m.expects('someMethod)().returning("foo")
m.someMethod shouldBe "foo"
}
}
如果没有,请查看ScalaMock代理模拟单元测试以获取更多示例。
我认为它应该更像:
import org.scalamock.scalatest.MockFactory
class MyTest extends FlatSpec with Matchers with MockFactory {
"MyInterface" should "work" in {
val m = mock[MyInterface]
(m.someMethod _).expects().returning("foo")
m.someMethod shouldBe "foo"
}
}
我认为预期arg是预期函数的arg
我使用scalaMock
版本4.1.0
,这对我有效:
对于某些特征:
trait MyInterface { def someMethod(n1: Int, n2: Int) }
这应该放入测试中
val myInterfaceMock = mock[MyInterface]
myInterfaceMock.someMethod _ expects (1,2)
更多阅读:scalaMockGuide,你会在那里找到一些例子