Currently, emitsExactly takes only a list of states as an argument. This is enough for simple cases, however I'm using classes to represent bloc's events and states.
Running the following test:
test('Should increment', () async {
final counterBloc = CounterBloc();
counterBloc.add(Increment());
await emitsExactly(counterBloc, [
InitialCounterState(),
CounterDecremented(),
]);
});
results with the following error:
ERROR: Expected: [Instance of 'InitialCounterState', Instance of 'CounterIncremented']
Actual: [Instance of 'InitialCounterState', Instance of 'CounterIncremented']
Which: was <Instance of 'InitialCounterState'> instead of <Instance of 'InitialCounterState'> at location [0]
That's because instances of states emitted by bloc and instances of states passed to emitsExactly are not equal.
I'm aware that I can solve this problem by extending Equatable class from equatable package, however I want my instances of the same state to be distinguishable.
Describe the solution you'd like
I would like to pass iterable of matchers to emitsExactly instead of list of states.
Future<void> emitsExactly<B extends Bloc<dynamic, State>, State>(
B bloc,
Iterable matchers,
) async {
assert(bloc != null);
final states = <State>[];
final subscription = bloc.listen(states.add);
await bloc.close();
expect(states, matchers);
subscription.cancel();
}
and to take advantage of isA<State> TypeMatcher:
test('Should increment', () async {
final counterBloc = CounterBloc();
counterBloc.add(Increment());
await emitsExactly(counterBloc, [
isA<InitialCounterState>(),
isA<CounterIncremented>(),
]);
});
By doing so, emitsExactly
emitsInOrder, emitsAnyOf, etc., that take Iterable matchers as an argument,test('Should increment', () async {
final counterBloc = SimpleCounterBloc();
counterBloc.add(SimpleCounterEvent.increment);
await emitsExactly(counterBloc, [0, 1]);
});
Here you can find the complete example portraying above's issue:
https://github.com/korzonkiee/emits_exactly_matchers/blob/master/test/counter_bloc_test.dart
I'm willing to create a pull request for this change. Just let me know if you are okey with it.
Hi @korzonkiee 馃憢
Thanks for opening an issue!
I think your proposal makes sense and if you want to open a PR that would be awesome 馃憤
Thanks!
Added in #696 and published in v2.2.0
Most helpful comment
Added in #696 and published in v2.2.0