Scalamock:如何获得"期望"代理模拟?

时间:2015-04-24 07:28:32

标签: scala scalatest scalamock

我正在使用Scalamock与ScalaTest,我正在尝试模拟Java接口。我目前有:

private val _iface = mock [MyInterface]

现在我想做

_iface expects `someMethod returning "foo" once

但编译器找不到expects

我导入了org.scalatest._org.scalamock.scalatest._。我还缺少什么?

3 个答案:

答案 0 :(得分:1)

首先,ScalaMock 3中不支持代理模拟,我认为它们将在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 proxy mocks unit tests了解更多示例。

答案 1 :(得分:1)

我认为它应该更像是:

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到函数

答案 2 :(得分:1)

我使用scalaMock4.1.0,这对我有用:

对于某些特征:

trait MyInterface { def someMethod(n1: Int, n2: Int) }

应该对此进行测试

val myInterfaceMock = mock[MyInterface]

myInterfaceMock.someMethod _ expects (1,2)

要了解更多信息,请访问scalaMock Guide