我将Scala 2.10
与ScalaMock 3.6
一起使用。
我有一个非常简单的测试用例,有4个测试场景。我为这些测试创建了一个mock
对象(模仿文件系统):
class ProcessingOperatorTest extends FlatSpec with Matchers with BeforeAndAfterEach with MockFactory {
...
val fakeFS = mock[FileIO]
(fakeFS.createFile _).expects(*).returns(true).anyNumberOfTimes()
(fakeFS.exist _).expects(where { (p: String) => p.contains(existing) }).returns(true).anyNumberOfTimes()
(fakeFS.exist _).expects(where { (p: String) => p.contains(notExisting) }).returns(false).anyNumberOfTimes()
behavior of "Something"
it should "test 1" in {
...
}
it should "test 2" in {
...
}
it should "test 3" in {
...
}
it should "test 4" in {
...
}
现在:
existing
模拟方法existing
和not existing
模拟方法createFile
)现在,出于某种原因,当同时运行所有这些测试时,第4次测试失败,给我以下错误。如果单独运行,它将通过。
Unexpected call: <mock-1> FileIO.exist(notExisting)
Expected:
inAnyOrder {
}
Actual:
<mock-1> FileIO.exist(notExisting)
ScalaTestFailureLocation: scala.Option at (Option.scala:120)
org.scalatest.exceptions.TestFailedException: Unexpected call: <mock-1> FileIO.exist(notExisting)
...
另一个步骤是将mock
声明及其行为复制粘贴到第4 it should { ... }
个测试场景中。然后测试工作(分开,一起)。
为什么全局mock
实例失败?
如果需要,我可以尝试将类似的测试场景准备为单独的sbt
项目。
答案 0 :(得分:4)
按here所述的org.scalatest.OneInstancePerTest
混合:
class ProcessingOperatorTest extends FlatSpec
with Matchers
with BeforeAndAfterEach
with MockFactory
with OneInstancePerTest {
...
}