Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] Android unit testing for flow with emitAll

Posted on November 11, 2022 By

Solution 1 :

Thanks to gpunto , this is the solution he suggested

@Test
fun testLoadMovies() = runTest {
    whenever(useCase.execute()).thenReturn(flow {
         MoviesStates.EmptyList
    })

    useCase.execute().collectLatest { states ->
        val actual = viewModel.handle(MoviesActions.LoadMovies).drop(1).first()
        val expected = states 
        assertEquals(expected, actual)
    }
}

Problem :

I have a function that return flow by emitAll

 fun handle(actions: MoviesActions): Flow<MoviesStates> = flow {
    when (actions) {
        is MoviesActions.LoadMovies -> {
            emit(MoviesStates.Loading)
            emitAll(moviesUseCase.execute())
        }
    }
}

And this the use case function

suspend fun execute(): Flow<MoviesStates> = flow {
    combine(f1, f2) { state1: MoviesStates, state2: MoviesStates ->
     
          // some code
        
    }.collect {
        emit(it)
    }
}

No problem in testing the first emission MoviesStates.Loading, the problem is when I try to test the flow which return from usecase by emitAll emitAll(moviesUseCase.execute()), the test fails and I got this result

java.util.NoSuchElementException: Expected at least one element

this is my unit test

@Test
fun testLoadMovies() = runBlocking {
    whenever(useCase.execute()).thenReturn(flow {
        MoviesStates.EmptyList
    })

    val actual = viewModel.handle(MoviesActions.LoadMovies).drop(1).first()
    val expected = MoviesStates.EmptyList

    assertEquals(actual, expected)
}

So How can I test it correctly?

Comments

Comment posted by turbine

You should collect the emissions in the test and then assert on what you collected. If you don’t want to do it manually, I suggest you try

READ  [FIXED] Android studio 3.6 give an redline for android_screenOrientation="portrait", need to change android_screenOrientation="fullSensor"?
Powered by Inline Related Posts
Android Tags:android, kotlin, kotlin-coroutines, kotlin-flow, unit-testing

Post navigation

Previous Post: [FIXED] android – Overlapping items in RelativeLayout with ImageView and TextView
Next Post: [FIXED] android – Unable to instantiate application com.company.app.MainApplication: java.lang.ClassNotFoundException React Native Expo

Related Posts

[FIXED] android – Send an image through socket: ByteArrayOutputStream.size() return negative value (sometimes ) Android
[FIXED] webpack – ‘Expo not defined’ in React Native Android
[FIXED] android – Kotlin App Crash if back button pressed during fetching API Android
[FIXED] java – Android Studio Null object Reference Android
[FIXED] java – How to take a photo in Android, using full image size(not thumbnail) and compress the image to bytes? Android
[FIXED] android – Flutter: i get invalid argument(s) Error in flutter if i want to navigate to detail page Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme