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 : 28 : /// {@template mock_cubit} 29 : /// Extend or mixin this class to mark the implementation as a [MockCubit]. 30 : /// 31 : /// A mocked cubit implements all fields and methods with a default 32 : /// implementation that does not throw a [NoSuchMethodError], 33 : /// and may be further customized at runtime to define how it may behave using 34 : /// [when] and `whenListen`. 35 : /// 36 : /// _**Note**: It is critical to explicitly provide the state 37 : /// types when extending [MockCubit]_. 38 : /// 39 : /// **GOOD** 40 : /// ```dart 41 : /// class MockCounterCubit extends MockCubit<int> 42 : /// implements CounterCubit {} 43 : /// ``` 44 : /// 45 : /// **BAD** 46 : /// ```dart 47 : /// class MockCounterCubit extends MockBloc implements CounterCubit {} 48 : /// ``` 49 : /// {@endtemplate} 50 : class MockCubit<S> extends _MockBlocBase<S> implements Cubit<S> {} 51 : 52 : class _MockBlocBase<S> extends Mock implements BlocBase<S> { 53 1 : _MockBlocBase() { 54 6 : when(() => stream).thenAnswer((_) => Stream<S>.empty()); 55 5 : when(close).thenAnswer((_) => Future<void>.value()); 56 : } 57 : }