Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

307
Visualizações
The TestScope advanced example of usage in tests with coroutines

Could someone provide an advanced example of using the new TestScope and TestScope.launch while testing with coroutines?

It seems like in the new kotlinx.coroutines.test release they've added somewhat called TestScope to the library. Also, they've deprecated the old TestCoroutineDispatcher and told to use TestScope.runTests instead, however, they didn't add much documentation on how to use it. Everything I could find is this.

Can anyone provide me with some extra examples of usage in different scenarios?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I could also fix this problem with @Joffrey mentioned at the comment. You only need to use runTest { } to use coroutine scope at the test code. By using runTest { } you can use TestScope inside the block with this.

In my case, there was a flow function at the viewModel something like this. It was running inside the viewModelScope.

    fun getFriendDataWithFlow() {
        viewModelScope.launch {
            repository.loadFriendsWithFlow()
            ...
        }
     }

And I should test this at my test code, and I used it like this.

   @ExperimentalCoroutinesApi
   @Before
   fun setup() {
       Dispatchers.setMain(StandardTestDispatcher())
   }

   @ExperimentalCoroutinesApi
   @Test
   fun temp() {
        runTest {
            viewModel.getFriendDataWithFlow()
        }
   }

All I did was add runTest { } block, and add coroutine code inside the block. And it worked for me. My test coroutine dependency was "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0-RC".

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda