I am using Scala 2.10
with ScalaMock 3.6
.
I have a quite simple test case with 4 test scenarios. I have created a mock
object for those tests to use (imitating file system):
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 {
...
}
Now:
- 1st test does not use any of mocked methods (but needs the mock object)
- 2nd test uses only
existing
mock method - 3rd test uses both
existing
andnot existing
mock methods - 4th test uses all methods, (also
createFile
)
Now, for some reason, when running all those tests together, 4th test fails giving me the following error. If running separately, it will pass.
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)
...
The other walkaround is to copy-paste the mock
declaration and its behaviour inside 4th it should { ... }
test scenario. Tests work then (separately, and all together).
Why is global mock
instance failing?
I can try to prepare a similar test scenario as separate sbt
project if needed.