I have a question regarding mockk
.
When using MockK's wasNot called
to check that ClassA
is called, there is a problem that the test fails if it is called in another test even if ClassA
is not called in the current test.
Why is this happening and how can I fix it?
Code:
class ClassA {
fun method(): Int = 1
}
class ClassB(val classA: ClassA, var call: Boolean = true) {
fun method(): Int = if(call) classA.method() else 0
}
class MyTest: FreeSpec({
"Test" - {
val mockClassA = mockk<ClassA>()
val sut = ClassB(mockClassA)
"methodA called" - {
every { mockClassA.method() } returns 1
sut.method()
verify { mockClassA.method() }
}
"methodA not called" - {
every { mockClassA.method() } returns 1
sut.call = false
sut.method()
verify { mockClassA wasNot called } // failed here
}
}
})
Error message:
Verification failed: ClassA(#1) should not be called: