https://kotlinlang.org logo
#mockk
Title
# mockk
s

svenjacobs

06/12/2019, 9:57 AM
Can someone please explain this behaviour to me?
Copy code
class CouponListInteractor(
    private val isCouponSelectedUseCase: IsCouponSelectedUseCase
) {

    suspend fun isCouponSelected(uuid: String) = isCouponSelectedUseCase.execute(uuid)
}

object CouponListInteractorTest : Spek(
    {
        val isCouponSelectedUseCase by memoized {
            mockk<IsCouponSelectedUseCase> {
                coEvery { execute(any()) } returns false
                coEvery { execute("uuid") } returns true
            }
        }
        
        val interactor = CouponListInteractor(
            isCouponSelectedUseCase
        )

        describe("CouponListInteractor") {

        context("isCouponSelected") {

            it("should return coupon selected state") {
                runBlockingTest {
                    interactor.isCouponSelected("uuid") shouldEqual true
                }

                coVerify {
                    // ERROR: Bad recording sequence. State: AnsweringState 
                    isCouponSelectedUseCase.execute("uuid")
                }
            }
        }
    }
}
coVerify
produces
Bad recording sequence. State: AnsweringState
. However when I replace
interactor.isCouponSelected("uuid") shouldEqual true
with
isCouponSelectedUseCase.execute("uuid") shouldEqual true
the test runs.
isCouponSelected()
just delegates to the use case. I don't understand this behaviour? 🤯
Sorry, my bad. It had actually nothing to do with MockK. The problem was that I forgot to put
by memoized {}
around
CouponListInteractor
so the same instance was used for all tests and something got mixed up internally.
346 Views