Testing run effect using WithTaskGroup/async let #1506
-
What is the best way to test receiving actions when the reducer uses I have tried putting in delays in each of the dependency functions used in the I've tried using a timeout and while that works, the state assertions come back wrong because another effect can change the state in a different order. I was looking for examples from isowords but not finding anything. Anyone have any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @PorterHoskins, this is definitely a bit of a blind spot in the library right now, and this exact use case was one of the motivating reasons we started a forum discussion that sadly didn't really go anywhere. Stephen and I have discussed potentially adding a await store.send(.buttonTapped)
await store.receive(inAnyOrder: [.responseA, .responseB]) {
// state assertion
} This could help with testing parallel effects that can emit in any order. It still wouldn't be a 100% solution because if the mutations that happen in Even before we add that tool, there is a way to force the order in your test right now. You can use a test scheduler directly in the test to force one affect to take 1 second and the other effect to take 2 seconds. That gives you a deterministic way to sequence the effects: let testScheduler = DispatchQueue.test
let store = TestStore(...)
store.dependencies.fetchA = {
try await testScheduler.sleep(for: .seconds(1))
return // mock data
}
store.dependencies.fetchB = {
try await testScheduler.sleep(for: .seconds(2))
return // mock data
}
await store.send(.buttonTapped)
await testScheduler.advance(by: .seconds(1))
await store.receive(.responseA)
await testScheduler.advance(by: .seconds(2))
await store.receive(.responseB) Hopefully that helps. |
Beta Was this translation helpful? Give feedback.
Hi @PorterHoskins, this is definitely a bit of a blind spot in the library right now, and this exact use case was one of the motivating reasons we started a forum discussion that sadly didn't really go anywhere.
Stephen and I have discussed potentially adding a
receive
method onTestStore
that allows you to receive multiple actions in any order. It might look like this:This could help with testing parallel effects that can emit in any order. It still wouldn't be a 100% solution because if the mutations that happen in
.responseA
and.responseB
overlap at all, then the order o…