LCOV - code coverage report
Current view: top level - src - mock_bloc.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 21 21 100.0 %
Date: 2021-04-25 20:36:08 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:bloc/bloc.dart';
       2             : import 'package:mocktail/mocktail.dart';
       3             : 
       4             : /// {@template mock_bloc}
       5             : /// Extend or mixin this class to mark the implementation as a [MockBloc].
       6             : ///
       7             : /// A mocked bloc implements all fields and methods with a default
       8             : /// implementation that does not throw a [NoSuchMethodError],
       9             : /// and may be further customized at runtime to define how it may behave using
      10             : /// [when] and `whenListen`.
      11             : ///
      12             : /// _**Note**: It is critical to explicitly provide the event and state
      13             : /// types when extending [MockBloc]_.
      14             : ///
      15             : /// **GOOD**
      16             : /// ```dart
      17             : /// class MockCounterBloc extends MockBloc<CounterEvent, int>
      18             : ///   implements CounterBloc {}
      19             : /// ```
      20             : ///
      21             : /// **BAD**
      22             : /// ```dart
      23             : /// class MockCounterBloc extends MockBloc implements CounterBloc {}
      24             : /// ```
      25             : /// {@endtemplate}
      26             : class MockBloc<E, S> extends _MockBlocBase<S> implements Bloc<E, S> {
      27             :   /// {@macro mock_bloc}
      28           1 :   MockBloc() {
      29           8 :     when(() => mapEventToState(any())).thenAnswer((_) => Stream<S>.empty());
      30           6 :     when(() => add(any())).thenReturn(null);
      31             :   }
      32             : }
      33             : 
      34             : /// {@template mock_cubit}
      35             : /// Extend or mixin this class to mark the implementation as a [MockCubit].
      36             : ///
      37             : /// A mocked cubit implements all fields and methods with a default
      38             : /// implementation that does not throw a [NoSuchMethodError],
      39             : /// and may be further customized at runtime to define how it may behave using
      40             : /// [when] and `whenListen`.
      41             : ///
      42             : /// _**Note**: It is critical to explicitly provide the state
      43             : /// types when extending [MockCubit]_.
      44             : ///
      45             : /// **GOOD**
      46             : /// ```dart
      47             : /// class MockCounterCubit extends MockCubit<int>
      48             : ///   implements CounterCubit {}
      49             : /// ```
      50             : ///
      51             : /// **BAD**
      52             : /// ```dart
      53             : /// class MockCounterCubit extends MockBloc implements CounterCubit {}
      54             : /// ```
      55             : /// {@endtemplate}
      56             : class MockCubit<S> extends _MockBlocBase<S> implements Cubit<S> {}
      57             : 
      58             : class _MockBlocBase<S> extends Mock implements BlocBase<S> {
      59           1 :   _MockBlocBase() {
      60           1 :     registerFallbackValue<void Function(S)>((S _) {});
      61           1 :     registerFallbackValue<void Function()>(() {});
      62           2 :     when(
      63             :       // ignore: deprecated_member_use
      64           2 :       () => listen(
      65           1 :         any(),
      66           1 :         onDone: any(named: 'onDone'),
      67           1 :         onError: any(named: 'onError'),
      68           1 :         cancelOnError: any(named: 'cancelOnError'),
      69             :       ),
      70           2 :     ).thenAnswer((invocation) {
      71           2 :       return Stream<S>.empty().listen(
      72           2 :         invocation.positionalArguments.first as void Function(S data),
      73           2 :         onError: invocation.namedArguments[#onError] as Function?,
      74           2 :         onDone: invocation.namedArguments[#onDone] as void Function()?,
      75           2 :         cancelOnError: invocation.namedArguments[#cancelOnError] as bool?,
      76             :       );
      77             :     });
      78           7 :     when(() => stream).thenAnswer((_) => Stream<S>.empty());
      79           6 :     when(close).thenAnswer((_) => Future<void>.value());
      80           6 :     when(() => emit(any())).thenReturn(null);
      81             :   }
      82             : }

Generated by: LCOV version 1.15