I'm writing a test using flutter_bloc and the bloc_test libraries, which looks something like this:
blocTest(
'description',
build: () => myBloc,
act: (bloc) {
bloc.add(SomeEvent(stuff));
},
expect: [firstState, secondState],
)
Unfortunately, when myBloc calls out to the repository, the repository awaits on a gRPC call (to a fake gRPC service), and the expect call runs before myBloc has a chance to emit any states. How should I handle this?
Ideas I've considered:
- Set blocTest.wait to some Duration. This works, but it's hacky! and slow!
- Mock my repository. I tend to dislike this approach, because the repository outputs multiple values for each update. The interactions are pretty verbose to mock in every test. Instead, I opted to mock the data sources the repository talks to, by writing a fake gRPC service.
- Write something so I can change the
actcallback to look something like this:
act: (bloc) async {
// I don't know what this could be yet. Some setup for later.
bloc.onEvent(doSomethingSmart);
bloc.add(SomeEvent(stuff));
// Block until we have some set of states, but this is basically what expect does.
await somethingSmart.expect([firstState, secondState]);
// or alternatively, just wait for N events
await somethingSmart.expectNumEvents(2);
},
Is there something like this already? Are there other suggestions for how to handle this?