Skip to content

Commit d9c0d81

Browse files
committed
Fix test falling due to unexpected mockk behaviour (looks like a bug)
1 parent 8e92214 commit d9c0d81

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

tests/src/commonTest/kotlin/ru/nsk/kstatemachine/state/StateCleanupTest.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,45 @@ package ru.nsk.kstatemachine.state
1010
import io.kotest.core.spec.style.FreeSpec
1111
import io.kotest.matchers.shouldBe
1212
import io.mockk.coVerify
13-
import io.mockk.spyk
13+
import io.mockk.mockk
1414
import ru.nsk.kstatemachine.CoroutineStarterType
1515
import ru.nsk.kstatemachine.createTestStateMachine
1616
import ru.nsk.kstatemachine.state.StateCleanupTestData.State1
1717
import ru.nsk.kstatemachine.statemachine.destroyBlocking
1818

1919
private object StateCleanupTestData {
20-
class State1 : DefaultState("state1")
20+
class State1(private val onCleanupListener: () -> Unit) : DefaultState("state1") {
21+
override suspend fun onCleanup() {
22+
super.onCleanup()
23+
onCleanupListener()
24+
}
25+
}
2126
}
2227

2328
class StateCleanupTest : FreeSpec({
2429
CoroutineStarterType.entries.forEach { coroutineStarterType ->
2530
"$coroutineStarterType" - {
2631
"cleanup is not called" {
27-
val state = spyk<State1>()
32+
val listener = mockk<() -> Unit>(relaxed = true)
33+
val state = State1(listener)
2834
useInMachine(coroutineStarterType, state)
29-
coVerify(inverse = true) { state.onCleanup() }
35+
coVerify(inverse = true) { listener() }
3036
}
3137

3238
"cleanup is called on machine manual destruction" {
33-
val state = spyk<State1>()
39+
val listener = mockk<() -> Unit>(relaxed = true)
40+
val state = State1(listener)
3441
useInMachine(coroutineStarterType, state).destroyBlocking()
35-
coVerify(exactly = 1) { state.onCleanup() }
42+
coVerify(exactly = 1) { listener() }
3643
}
3744

3845
"cleanup is called on machine auto destruction" {
39-
val state = spyk<State1>()
46+
val listener = mockk<() -> Unit>(relaxed = true)
47+
val state = State1(listener)
4048
val machine1 = useInMachine(coroutineStarterType, state)
4149
val machine2 = useInMachine(coroutineStarterType, state)
4250

43-
coVerify(exactly = 1) { state.onCleanup() }
51+
coVerify(exactly = 1) { listener() }
4452
machine1.isDestroyed shouldBe true
4553
machine2.isDestroyed shouldBe false
4654
}

0 commit comments

Comments
 (0)