Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: how to mock Bloc

I would like to mock my Bloc in order to test my view.

For example, this is my Bloc:

class SearchBloc extends Bloc<SearchEvent, SearchState> {
  @override
  // TODO: implement initialState
  SearchState get initialState => SearchStateUninitialized();

  @override
  Stream<SearchState> mapEventToState(SearchState currentState, SearchEvent event) async* {
    if (event is UserWrites) {
      yield (SearchStateInitialized.success(objects);
    }
  }
}

And this is the view:

class _SearchViewState extends State<SearchView> {
  final TextEditingController _filterController = new TextEditingController();

  @override
  void initState() {
    _filterController.addListener(() {
    widget._searchBloc.dispatch(FetchByName(_filterController.text));
     }
  }


 TextField buildAppBarTitle(BuildContext context) {
    return new TextField(
      key: Key("AppBarTextField"),
        controller: _filterController,
    );
  }

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: buildAppBarTitle(context),),
      body: buildBlocBuilder(),
    );
  }

BlocBuilder<SearchEvent, SearchState> buildBlocBuilder() {
    return BlocBuilder(
        bloc: widget._searchBloc,
        builder: (context, state) {
          if (state is SearchStateUninitialized) {
            return Container(
              key: Key("EmptyContainer"),
            );
          } 
            return buildInitializedView(state, context);
          }
        });

buildInitializedView(SearchStateInitialized state, BuildContext context) {
    if (state.objects.isEmpty) {
      return Center(child: Text("Nothing found"),);
    } else {
      return buildListOfCards();
    }
  }
    }

Now, this is my test:

testWidgets('Should find a card when the user searches for something', (WidgetTester tester) async {

  _searchView = new SearchView(_searchBloc);

    when(mockService.find( name: "a")).thenAnswer((_) =>
    [objects]);

    await tester.pumpWidget(generateApp(_searchView));
    await tester.enterText(find.byKey(Key("searchBar")), "a");
    await tester.pump();

    expect(find.byType(Card), findsOneWidget);
  });
}

As you can see, I just want to test that, when the user writes something in the search, and the object he's looking for exists, a card should be shown.

like image 919
Little Monkey Avatar asked Mar 19 '19 08:03

Little Monkey


People also ask

How do you use mock in flutter?

Repository tests using Mockito By annotating a library element (such as a test file's main function, or a class) with @GenerateMocks , you are directing Mockito's code generation to write a mock class for each "real" class listed, in a new library. The generated mocks will be located in home_repo_test. mocks. dart .

How do you test a cubit in flutter?

verify is called with the cubit returned by build . group('CounterCubit', () { cubitTest( 'emits [] when nothing is called', build: () async => CounterCubit(), expect: [], ); cubitTest( 'emits [1] when increment is called', build: () async => CounterCubit(), act: (cubit) async => cubit. increment(), expect: [1], ); });


1 Answers

Have a look at a post from David Anaya which deal with Unit Testing with “Bloc” and mockito.

The last version of his example is here

like image 102
hawkbee Avatar answered Oct 22 '22 14:10

hawkbee