我将 Scalamock 与 ScalaTest 一起使用,并试图模拟 Java 接口。我目前有:
private val _iface = mock [MyInterface]
现在我想做
_iface expects `someMethod returning "foo" once
但编译器没有找到expects
.
我导入org.scalatest._
和org.scalamock.scalatest._
. 我还缺少什么?
First of all, proxy mocks are not supported very well in ScalaMock 3, and I think they will be completely removed in ScalaMock 4. Do you really need to use proxy mocks instead macro mocks?
This should work:
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"
}
}
If not, please check ScalaMock proxy mocks unit tests for more examples.
我使用scalaMock
version 4.1.0
,这对我有用:
对于某些特征:
trait MyInterface { def someMethod(n1: Int, n2: Int) }
这应该投入测试
val myInterfaceMock = mock[MyInterface]
myInterfaceMock.someMethod _ expects (1,2)
更多阅读:scalaMock Guide,你会在那里找到一些例子
我认为它应该更像是:
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 到函数