简体   繁体   中英

How to verify Flutter Bloc added event in widget test?

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM