bloc_test 6.0.0 copy "bloc_test: ^6.0.0" to clipboard
bloc_test: ^6.0.0 copied to clipboard

outdated

A testing library which makes it easy to test blocs. Built to be used with the bloc state management package.

example/main.dart

import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:test/test.dart';

enum CounterEvent { increment }

class CounterBloc extends Bloc<CounterEvent, int> {
  CounterBloc() : super(0);

  @override
  Stream<int> mapEventToState(CounterEvent event) async* {
    switch (event) {
      case CounterEvent.increment:
        yield state + 1;
        break;
    }
  }
}

// Mock Bloc
class MockCounterBloc extends MockBloc<CounterEvent, int>
    implements CounterBloc {}

void main() {
  group('whenListen', () {
    test("Let's mock the CounterBloc's stream!", () {
      // Create Mock CounterBloc Instance
      final counterBloc = MockCounterBloc();

      // Stub the listen with a fake Stream
      whenListen(counterBloc, Stream.fromIterable([0, 1, 2, 3]));

      // Expect that the CounterBloc instance emitted the stubbed Stream of
      // states
      expectLater(counterBloc, emitsInOrder(<int>[0, 1, 2, 3]));
    });
  });

  group('emitsExactly', () {
    test('emits [] when nothing is added', () async {
      final bloc = CounterBloc();
      await emitsExactly(bloc, []);
    });

    test('emits [1] when CounterEvent.increment is added', () async {
      final bloc = CounterBloc();
      bloc.add(CounterEvent.increment);
      await emitsExactly(bloc, [1]);
    });
  });

  group('blocTest', () {
    blocTest(
      'emits [] when nothing is added',
      build: () async => CounterBloc(),
      expect: [],
    );

    blocTest(
      'emits [1] when CounterEvent.increment is added',
      build: () async => CounterBloc(),
      act: (bloc) => bloc.add(CounterEvent.increment),
      expect: [1],
    );
  });
}
561
likes
0
pub points
98%
popularity

Publisher

verified publisherbloclibrary.dev

A testing library which makes it easy to test blocs. Built to be used with the bloc state management package.

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

bloc, cubit_test, meta, test

More

Packages that depend on bloc_test