Skip to content

Conversation

@GabrielBrasileiro
Copy link

Hi, I’m Gabriel.

If possible, I would like to propose a function that has helped me validate the strict order of emissions across multiple flows.

While reading the documentation, I found the recommended way to test each flow individually:

turbineScope {
    val turbine1 = flowOf(1).testIn(backgroundScope)
    val turbine2 = flowOf(2).testIn(backgroundScope)
    assertEquals(1, turbine1.awaitItem())
    assertEquals(2, turbine2.awaitItem())
}

However, with this approach, if I need to validate the strict order of emissions across two flows, such as SharedFlow and StateFlow inside a ViewModel, I only have the option to check them separately.

The problem is that if the production code accidentally changes the order of events, the test still succeeds.

Example (production code):

	1.	flowA emits "A1"
	2.	flowB emits 1
	3.	flowA emits "A2"

Test:

	1.	await "A1" from flowA
	2.	await 1 from flowB
	3.	await "A2" from flowA

But if production code changes to:

	1.	flowA emits "A1"
	2.	flowA emits "A2"
	3.	flowB emits 1

The test continues to pass, even though the order is no longer correct.

With this MR, I would like to propose the turbineOf method, which merges multiple flows and allows validating the exact emission order. This function has been very helpful in my own projects, and I believe it would be useful for other developers as well.

Thank you in advance.

@JakeWharton
Copy link
Collaborator

I don't think we'll add this, mostly because it creates a second way of doing something and opens us up to needing to potentially support more mechanisms of combining flows in the future. Maybe someone wants merge behavior, but they also could want zip or a combine latest or some other policy. I think it's perfectly reasonable to combine flows in whatever way you want before calling .test { }, and that ensures that no one has to guess about what the behavior is because it's visible directly in the test instead of hidden behind a bespoke function from this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants