[英]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.然后,我想创建一个小部件测试场景,可以验证 bloc 是否添加了真实事件。 I created mock class like this:
我像这样创建了模拟 class :
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"但是,当我验证(使用mocktail )将事件添加到 bloc 时,结果是“没有匹配的调用”
verify(() => mockMyBloc.add(MyEvent())); // No matching calls
How I can verify bloc is adding true event?我如何验证 bloc 是否添加了真实事件? Thank you in advance!
先感谢您!
verify(() => mockMyBloc.add(MyEvent()));
looks correct.看起来正确。 Double check that you're tapping on the
ElevatedButton
during your test.仔细检查您在测试期间是否点击了
ElevatedButton
。 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
.此外,请确保在您的测试中为您的小部件提供了
MockMyBloc
块,并且MyEvent
扩展Equatable
以便MyEvent
的实例等于MyEvent
的另一个实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.