Search code examples
flutterunit-testingdartbloc

Dart tests: excpect stream to emit nothing


I want to check that some Stream (e.g. bloc) emits nothing during unit test.

More detailed description: I use mockito and flutter_bloc packages. System under test is a bloc. My test:

  test('sut emits nothing', () {
    final event = SomeEvent(false);
    sut.add(event);
    expectLater(
      sut,
      emitsNothing,    // <---- HERE I want to check that sut emits nothing
    );
  });

Here is mapping function of my bloc:

  @override
  Stream<State> mapEventToState(Event event) async* {
    if (event is SomeEvent) {
      // If `flag` is true - then bloc emits a state,
      // if `flag` is false - then bloc emits nothing.
      if (event.flag) {    
        yield State.someState;
      }
    }
  }

Here is code of SomeEvent:

abstract class Event extends Equatable {}

class SomeEvent extends Event {
  final bool flag;

  SomeEvent(this.flag);

  @override
  List<Object> get props => [flag];
}

Can I check it out? Any help appreciated.


Solution

  • you can achieve this by using bloc_test package.

    blocTest(
      'CounterBloc emits no events',
      build: () => CounterBloc(),
      act: (_) {}, // no action taken
      expect: [], // no event emitted
    );