简体   繁体   English

Flutter 测试表明 bloc 没有产生任何状态

[英]Flutter test that no state is yielded from bloc

I have a case when some condition is met, then the bloc is not yielding a state at all (ignoring all events).我有一个情况,当满足某些条件时,该集团根本不会产生状态(忽略所有事件)。 How can I test that no state is yielded when a new event is added to bloc ?如何测试在将新事件添加到 bloc 时没有产生任何状态?

I'm using flutter_bloc: ^3.1.0 and bloc_test: ^3.1.0我正在使用flutter_bloc: ^3.1.0bloc_test: ^3.1.0

      'test ignore all event after AlarmEvent()',
      () async {
        bloc.add(AlarmEvent());
        bloc.add(GetRandomPostEvent());

        await emitsExactly(
          bloc,
          [
            SplashScreenLoadingState(),
            ShowAlarmState(),
          ],
        );
      },
    );

As the feature is not implemented yet I'm expecting the test to fail but actually is runs successfully.由于该功能尚未实现,我预计测试会失败但实际上运行成功。 Any idea ?任何的想法 ?

You can make a test specifically for that condition that nothing should be emitted, and subscribe to your bloc, then call fail() in the subscription if the block emits, so you know they only way the test succeeded is if fail() wasn't called.您可以专门针对不应该发出任何内容的情况进行测试,然后订阅您的区块,然后在该区块发出时调用订阅中的fail() ,因此您知道测试成功的唯一方法是如果fail()不是' t叫。 This should be a synchronous call as well.这也应该是一个同步调用。

Something like:就像是:

var subscription = myBlocStream.listen((_) { 
  fail('Should not emit'); 
}); 
myBloc.doSomething();
subscription.cancel();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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