【发布时间】:2019-08-08 21:16:16
【问题描述】:
我正在为 PlayFramework Scala 应用程序创建单元测试,并遇到了一个我需要测试的函数,它会调用命令行接口。这个 cli 调用无法在我们的测试环境中运行,所以我想模拟一下。
class Foo @Inject()(val bar: Bar, val a: A, val b: B...) {
def testThis(...) = {
...
callCommandLine
...
}
}
class Bar() {
def callCommandLine(s: String): String = {
...
}
}
以下是我尝试过的
class FooSpec() {
"testFoo" in {
val foo = app.injector.instanceOf[Foo]
val result = testThis(...)
val bar = mock[Bar]
val mockedOutput = "fake cmd line result"
when(bar.callCommandLine(anyString)).thenReturn(mockedOutput)
result mustBe mockedOutput
}
}
我明白为什么我的测试不起作用,但我不知道我需要做什么才能使它起作用。我应该将模拟的 bar 类注入 foo 吗?
【问题讨论】:
标签: scala playframework mockito scalatest