-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use TestCoroutineScope, pass also TestCoroutineDispatcher to control …
…execution and delay, do not skip delays with runBlockingTest{} Kotlin/kotlinx.coroutines#1266
- Loading branch information
Showing
4 changed files
with
25 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 10 additions & 7 deletions
17
logic/src/main/java/xyz/kewiany/contactus/logic/MainLogic.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
package xyz.kewiany.contactus.logic | ||
|
||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.coroutines.* | ||
import xyz.kewiany.contactus.core.DefaultDispatcherProvider | ||
import xyz.kewiany.contactus.core.DispatcherProvider | ||
import xyz.kewiany.contactus.logic.common.MainViewState | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
suspend fun MainViewState.MainLogic(dispatchers: DispatcherProvider = DefaultDispatcherProvider()): Boolean { | ||
suspend fun MainViewState.MainLogic( | ||
dispatchers: DispatcherProvider = DefaultDispatcherProvider() | ||
): Boolean { | ||
commonViewState.isLoading = true | ||
withContext(dispatchers.io()) { | ||
delay(5000) | ||
commonViewState.isLoading = false | ||
} | ||
withContext(dispatchers.io()) { | ||
delay(1000) | ||
commonViewState.isLoading = false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 11 additions & 8 deletions
19
logic/src/test/java/xyz/kewiany/contactus/MainLogicTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,25 @@ | ||
package xyz.kewiany.contactus | ||
|
||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.test.TestCoroutineScope | ||
import kotlinx.coroutines.test.runBlockingTest | ||
import kotlinx.coroutines.launch | ||
import xyz.kewiany.contactus.logic.MainLogic | ||
import xyz.kewiany.contactus.logic.common.MainViewState | ||
|
||
internal class MainLogicTest : CustomFreeSpec({ | ||
val state = MainViewState() | ||
val stateT = MainStateT(state) | ||
|
||
beforeTest { | ||
testDispatcher.runBlockingTest { | ||
"test loading" - { | ||
testScope.launch { | ||
state.MainLogic(testDispatcherProvider) | ||
} | ||
"load" { | ||
stateT.state.commonViewState.isLoading shouldBe true | ||
} | ||
testScope.advanceTimeBy(1000) | ||
"do not load" { | ||
stateT.state.commonViewState.isLoading shouldBe false | ||
} | ||
} | ||
}) | ||
|
||
"test" - { | ||
stateT.state.commonViewState.isLoading shouldBe false | ||
} | ||
}) |