I have not found a way to write an assertion verifying that a mocked class's function was not called. When I write the assertion it complains that the function was not stubbed. But that function was never called in my test or implementation.
Is there another way to write this test?
In the example below I expected both tests to pass but the 2nd test fails.
import io.mockk.Called
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
class Adder {
fun addOne(value: Int) = value + 1
fun addTwo(value: Int) = value + 2
}
class BusinessMachine(private val adder: Adder) {
fun doBusiness(): Int {
val money = 100
return adder.addOne(money)
}
}
object BusinessMachineTest : Spek({
describe("BusinessMachine") {
it("should use addOne to do business") {
val adder = mockk<Adder>()
val subject = BusinessMachine(adder)
every { adder.addOne(any()) } returns 1
subject.doBusiness()
verify { adder.addOne(any()) }
}
it("should not use addTwo to do business") {
val adder = mockk<Adder>()
val subject = BusinessMachine(adder)
every { adder.addOne(any()) } returns 1
subject.doBusiness()
verify { adder::addTwo wasNot Called }
}
}
})
can't find stub fun com.distribrewtion.api.users.mailchimp.Adder.addTwo(kotlin.Int): kotlin.Int
io.mockk.MockKException: can't find stub fun com.distribrewtion.api.users.mailchimp.Adder.addTwo(kotlin.Int): kotlin.Int
at io.mockk.impl.stub.StubRepository.stubFor(StubRepository.kt:16)
at io.mockk.impl.recording.states.VerifyingState.checkWasNotCalled(VerifyingState.kt:82)
at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:45)
at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60)
at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
at io.mockk.MockKDsl.internalVerify(API.kt:118)
at io.mockk.MockKKt.verify(MockK.kt:139)
at io.mockk.MockKKt.verify$default(MockK.kt:136)
at com.distribrewtion.api.users.mailchimp.BusinessMachineTest$1$1$3.invoke(mockkexample.kt:52)
at com.distribrewtion.api.users.mailchimp.BusinessMachineTest$1$1$3.invoke(mockkexample.kt:22)
at org.spekframework.spek2.runtime.scope.TestScopeImpl.execute(Scopes.kt:94)
at org.spekframework.spek2.runtime.Executor$execute$$inlined$executeSafely$lambda$1$1.invokeSuspend(Executor.kt:52)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
I think in your case you should use:
verify(exactly = 0) { adder.addTwo(any()) }
Construction like verify { mock wasNot Called } is used not for function but for whole mock
Thanks @sgulyaev, that works for me.
Most helpful comment
I think in your case you should use:
verify(exactly = 0) { adder.addTwo(any()) }
Construction like verify { mock wasNot Called } is used not for function but for whole mock