I've been working a bit more with running effects on background threads and was writing some tests for this behaviour. But a lot of these background run effects are of the .fireAndForget
variety.
This means that at the end of my tests I have to run something like mainScheduler.advance()
for the test to pass since there's still some of those in flight. I'd like to make this more precise in my test by intermittently checking for inflight effects. In TestStore
's private completed()
method there's a check for longLivingEffects
and if there still are any it fails. Could we maybe expose this behaviour to make tests more precise?
For example (Using a test from my AVFoundation post earlier today)
store.assert(
.send(.onAppear), // returns a concatenated effect The first one is received below the second one is fire and forget
.receive(
.cameraClient(.providePreviewLayer(caLayer)),
{ $0.previewLayer = caLayer }
),
.do { backgroundQueue.advance() },
.do { mainQueue.advance() }, // schedulers catch the in flight effect here
.send(.takePictureButtonTapped), // also sends a concatenated effect
.receive(
.cameraClient(.pictureTaken(Data())),
{
$0.picData = Data()
$0.isTaken = true
}
),
.send(
.retakePictureButtonTapped,
{ $0.isTaken = false }
),
.do { backgroundQueue.advance() },
.do { mainQueue.advance() }
)
It would be nice to add an assertion that no more effects are in flight after receiving the .providePreviewLayer
action since if I hadn't added the schedulers it would be unclear from the test if there would still be effects in flight.
First thoughts are a Step
static called .assertNoActionsInFlight
, but there are probably cleaner solutions.
Would like to hear if this is considered useful and any thoughts on implementing. Once there's some consensus on an API I'd love to make a PR.
Cheers!