question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TestCoroutineDispatcher

See original GitHub issue

I have a method with next piece of code

viewModelScope.launch(exceptionHandler) {
            val rateList = withContext(Dispatchers.IO) {
                buildExchangeRateUseCase.execute(selected)
            }
            rates.onNext(rateList)
}

and test is

 coEvery {
            buildExchangeRatesUseCaseMock.execute(selectedRate)
 } returns exchangeRates

viewModel = ExchangeRateLifecycleAwareViewModel(buildExchangeRatesUseCaseMock)

assertData(exchangeRates, viewModel.rates)

I have also called Dispatchers.setMain(testDispatcher)

The problem is code inside withContext() is called asynchronously after assertion happens. A code inside my exceptionHandler also is called after assertion in case I want to assert exception behavior. I was able to solve that using runBlocking instead of withContext. However, in documentation it is said runBlocking should not be used from a coroutine. Although, in coroutines basic guide you gave both these functions to jump between threads (https://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-dispatchers.html#jumping-between-threads).

Could you please give an idea, how to first execute all the code inside withContext() before assertion is called?

P.S. I have tried runBlockingTest() but it also has no effect

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
qwwdfsadcommented, Aug 20, 2019

DI is the only solution for now.

We have plans to focus on testability in 1.3.1 right after the flow release. It will include stabilization of test dispatcher and (probably) mockable default and IO dispatchers, similar to Main dispatched (Dispatchers.setMain in kotlinx-coroutines-test)

0reactions
dkhalanskyjbcommented, Nov 22, 2021

We thoroughly reworked the test module in https://github.com/Kotlin/kotlinx.coroutines/commit/2e25baeb29274853a1926f6bf7c3be761f306d72, so now it supports multithreading and waiting for external completions. It is still preferable to use DI since the delays in the tasks forked off to other dispatchers are not skipped, but at least it’s now possible to write tests while using third-party code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TestCoroutineDispatcher - GitHub Pages
CoroutineDispatcher that performs both immediate and lazy execution of coroutines in tests and uses a TestCoroutineScheduler to control its virtual clock.
Read more >
TestCoroutineDispatcher - Kotlin
Contributing to Kotlin · Releases · Press Kit · Security · Blog · Issue Tracker · Brand assets · Careers. Kotlin™ is protected...
Read more >
Module kotlinx-coroutines-test - GitHub
The virtual time is controlled by an entity called the TestCoroutineScheduler, which behaves as the shared source of virtual time. Several dispatchers can...
Read more >
Unit Testing Coroutine Suspend Functions using ... - craigrussell
This post describes how to use TestCoroutineDispatcher to write efficient and stable unit tests for code written with coroutines.
Read more >
Testing Kotlin coroutines on Android
runTest is a coroutine builder designed for testing. Use this to wrap any tests that include coroutines. Note that coroutines can be started...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found