在如何使用ScalaMock模拟类内的高阶函数方面,我需要一些帮助。
import org.scalamock.scalatest.MockFactory
import org.scalatest.{FlatSpec, Matchers}
class TestSpec extends FlatSpec with MockFactory with Matchers {
class Foo {
def foo(f: () ⇒ String) = "this is foo"
}
val fooMock = mock[Foo]
"Failing test" should "pass but doesn't" in {
(fooMock
.foo(_: () ⇒ String))
.expects({ () ⇒
"bar"
})
.returns("this is the test")
// will fail here
val result = fooMock.foo({ () ⇒
"bar"
})
assert(true)
}
"Passing test" should "that passes but I can't do it this way" in {
val f = { () ⇒
"bar"
}
(fooMock.foo(_: () ⇒ String)).expects(f).returns("this is the test")
val result = fooMock.foo(f)
result shouldBe "this is the test"
}
}正如您在上面的代码中所看到的,当您传入一个具有更高阶函数的值时,被模拟的函数工作得很好,但是如果您键入每个点,则不会工作。在我的用例中,我不能像在第二个测试中那样做
下面的是关于用例的更多信息,但对于回答这个问题并不完全必要。
这是一个简化的例子,但我需要一个方法,使前者的工作。原因是(我会尽我最大的努力解释这一点)我有一个A类正在接受测试。内部A是一个传递模拟类B的函数,基本上foo函数(如下面所示)在这个模拟B中,我不能像下面的第二个例子那样传递f。如果这没有任何意义的话,我可以试着完全复制。
我需要第一次测试才能工作
知道为什么会发生这种事吗?
如果您对我为什么需要这样做感到好奇,下面是一个更精确的示例,说明我是如何使用这个方法的:
import org.scalamock.scalatest.MockFactory
import org.scalatest.{FlatSpec, Matchers}
class TestSpec extends FlatSpec with MockFactory with Matchers {
class Foo {
def foo(f: () ⇒ String) = s"you sent in: ${f()}"
}
object Bar {
def bar(foo: Foo): String = {
val f = { () ⇒ "bar" }
foo.foo(f)
}
}
val fooMock = mock[Foo]
"Failing test" should "pass but doesn't" in {
(fooMock.foo(_: () ⇒ String))
.expects({ () ⇒
"bar"
})
.returns("this is the test")
// will fail here
val result = Bar.bar(fooMock)
assert(true)
}
}发布于 2018-06-24 22:14:39
这不起作用的原因在于Scala本身。参见这个scala repl:
scala> val f = { () ⇒ "bar" }
f: () => String = $$Lambda$1031/294651011@14a049f9
scala> val g = { () ⇒ "bar" }
g: () => String = $$Lambda$1040/1907228381@9301672
scala> g == f
res0: Boolean = false因此,在没有帮助的情况下,ScalaMock也不能比较相等的函数。我建议的是,如果您只对函数的结果感兴趣,可以使用谓词匹配,如本文所述:http://scalamock.org/user-guide/matching/。
在谓词中,您可以计算捕获的参数并检查返回值是否确实是bar。或者,如果您的函数更复杂,也许您甚至可以做一个mock[() => String]并比较匹配器中的引用等式?
https://stackoverflow.com/questions/50881645
复制相似问题