Advertisement
Guest User

Mock Flutter Bloc initial state using Mockito

a guest
Jul 9th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.71 KB | None | 0 0
  1. // Test Dependencies
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_bloc/flutter_bloc.dart';
  4. import 'package:flutter_test/flutter_test.dart';
  5. import 'package:mockito/mockito.dart';
  6.  
  7. // TodoList Dependencies
  8. import 'package:todolist/widgets/todo_list.dart';
  9. import 'package:todolist/blocs/todo_list/bloc.dart';
  10. import 'package:todolist/blocs/todo_list/state.dart';
  11.  
  12. class MockTodoListBloc extends Mock implements TodoListBloc {}
  13.  
  14. void main() {
  15.   // Testing the TodoList widget
  16.   group('TodoList', () {
  17.  
  18.     // Test how it reacts when the Bloc state is TodoLoadingState
  19.     group('TodoLoadingState', () {
  20.       testWidgets('should render a loading icon when handling the TodoLoadingState', (WidgetTester tester) async {
  21.         // Setup the mock bloc
  22.         TodoListBloc todoListBloc = MockTodoListBloc();
  23.  
  24.         // Mock the injected TodoListBloc state.
  25.         // See _BlocBuilderBaseState for how it uses currentState to setup the state
  26.         when(todoListBloc.currentState).thenReturn(TodoLoadingState());
  27.  
  28.         // Wrap our widget test with a BlocProvider since our widget depends
  29.         // on a BlocBuilder. Our mock TodoListBloc will be passed
  30.         await tester.pumpWidget(
  31.           MaterialApp(
  32.             home: BlocProvider<TodoListBloc>(
  33.               builder: (context) => todoListBloc,
  34.               child: TodoList(),
  35.             ),
  36.           )
  37.         );
  38.  
  39.         expect(find.byType(CircularProgressIndicator), findsOneWidget);
  40.       });
  41.     });
  42.  
  43.     // Test how it reacts when the Bloc state is TodoLoadingState
  44.     group('TodoLoadedState', () {
  45.       testWidgets('should render a ListView of ListTile todos when TodoLoadedState', (WidgetTester tester) async {
  46.         // Mock our todo list items
  47.         final List<String> todos = [ "Take out the trash", "Feed the cat" ];
  48.        
  49.         // Setup the mock bloc
  50.         TodoListBloc todoListBloc = MockTodoListBloc();
  51.  
  52.         // Mock the injected TodoListBloc state and pass our mock todo list items.
  53.         // See _BlocBuilderBaseState for how it uses currentState to setup the state
  54.         when(todoListBloc.currentState).thenReturn(TodoLoadedState(
  55.           todos: todos
  56.         ));
  57.  
  58.         // Wrap our widget test with a BlocProvider since our widget depends
  59.         // on a BlocBuilder. Our mock TodoListBloc will be passed
  60.         await tester.pumpWidget(
  61.           MaterialApp(
  62.             home: BlocProvider<TodoListBloc>(
  63.               builder: (context) => todoListBloc,
  64.               child: TodoList(),
  65.             ),
  66.           )
  67.         );
  68.  
  69.         expect(find.byType(ListView), findsOneWidget);
  70.         expect(find.byType(ListTile), findsNWidgets(todos.length));
  71.       });
  72.     });
  73.   });
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement