I have my use case that talks to repository and emits multiple data as below -
class MyUseCase(private val myRepoInterface: MyRepoInterface) : MyUseCaseInterface {
override fun performAction(action: MyAction) = flow {
emit(MyResult.Loading)
emit(myRepoInterface.getData(action))
}
}
The unit test for the same looks like this below -
class MyUseCaseTest {
@get:Rule
val instantExecutorRule = InstantTaskExecutorRule()
@Mock
private lateinit var myRepoInterface: MyRepoInterface
private lateinit var myUseCaseInterface: MyUseCaseInterface
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
myUseCaseInterface = MyUseCase(myRepoInterface)
}
@After
fun tearDown() {
}
@Test
fun test_myUseCase_returnDataSuccess() {
runBlocking {
`when`(myRepoInterface.getData(MyAction.GetData))
.thenReturn(MyResult.Data("data"))
// Test
val flow = myUseCaseInterface.performAction(MyAction.GetData)
flow.collect { data ->
Assert.assertEquals(data, MyResult.Loading)
Assert.assertEquals(data, MyResult.Data("data"))
}
}
}
}
After running the test I get this error -
java.lang.AssertionError:
Expected : MyResult$Loading@4f32a3ad
Actual : MyResult.Data("data")
How do I test the use case that emits multiple things?