Bloc: Allow emitsExactly to take iterable of matchers as an argument

Created on 25 Nov 2019  路  2Comments  路  Source: felangel/bloc

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

  • complies with other existing matchers like emitsInOrder, emitsAnyOf, etc., that take Iterable matchers as an argument,
  • still works for simple cases, like:
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.

enhancement bloc_test

Most helpful comment

Added in #696 and published in v2.2.0

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ricktotec picture ricktotec  路  3Comments

hivesey picture hivesey  路  3Comments

abinvp picture abinvp  路  3Comments

nhwilly picture nhwilly  路  3Comments

RobPFarley picture RobPFarley  路  3Comments