How to verify Flutter Bloc added event in widget test?

Viewed 41

I have button that when pressed is add a event to bloc like this :

ElevatedButton(
  onPressed: () {
    context.read<MyBloc>().add(MyEvent());
  },
  child: ...,
);

And then, I want to create a widget test scenario that can verify if bloc adding true event. I created mock class like this :

class MockMyBloc extends MockBloc<MyEvent, MyState>
    implements MyBloc {}

class FakeMyEvent extends Fake implements MyEvent {}

class FakeMyState extends Fake implements MyState {}

However, when I verify (using mocktail) the addition of the event to the bloc, the result is "No matching calls"

verify(() => mockMyBloc.add(MyEvent())); // No matching calls

How I can verify bloc is adding true event? Thank you in advance!

1 Answers

verify(() => mockMyBloc.add(MyEvent())); looks correct. Double check that you're tapping on the ElevatedButton during your test. Also, make sure that your widget is provided the MockMyBloc bloc in your test, and that MyEvent extends Equatable so that an instance of MyEvent is equal to another instance of MyEvent.

Related