@@ -10,37 +10,45 @@ package ru.nsk.kstatemachine.state
1010import io.kotest.core.spec.style.FreeSpec
1111import io.kotest.matchers.shouldBe
1212import io.mockk.coVerify
13- import io.mockk.spyk
13+ import io.mockk.mockk
1414import ru.nsk.kstatemachine.CoroutineStarterType
1515import ru.nsk.kstatemachine.createTestStateMachine
1616import ru.nsk.kstatemachine.state.StateCleanupTestData.State1
1717import ru.nsk.kstatemachine.statemachine.destroyBlocking
1818
1919private 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
2328class 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