首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flutter:单元测试Cubit问题

Flutter:单元测试Cubit问题
EN

Stack Overflow用户
提问于 2021-08-05 13:24:32
回答 1查看 567关注 0票数 0

我一直在尝试为我的cubit设置单元测试,但是块测试似乎有问题。只是尝试检查值的初始状态在常规测试中工作得很好,但是一旦我尝试用bloc test做同样的事情,它就会吐出fail,实际是一个空列表,我不确定我做错了什么。互联网上没有任何东西对这个错误有帮助。

代码语言:javascript
复制
//passes
test('initial state of SettingsCubit', () {
  expect(
      settingsCubit.state,
      SettingsState(
          notificationsEnabled: false,
          gpsEnabled: false,
          celsiusEnabled: false,
          expandNavigation: false,
          breakingNewsNotifications: false,
          trendingNotifications: false,
          liveRemindersNotifications: false,
          sportsNotifications: false));
});

//fails
blocTest(
  'initial state of SettingsCubit',
  build: () => SettingsCubit(),
  expect: () => [
    SettingsState(
      expandNavigation: false,
      gpsEnabled: false,
      celsiusEnabled: false,
      notificationsEnabled: false,
      breakingNewsNotifications: false,
      trendingNotifications: false,
      liveRemindersNotifications: false,
      sportsNotifications: false,
    )
  ],
);

错误:

代码语言:javascript
复制
package:bloc_test/src/bloc_test.dart 193:9   testBloc.<fn>
===== asynchronous gap ===========================
dart:async                                   _asyncThenWrapperHelper
package:bloc_test/src/bloc_test.dart         testBloc.<fn>
dart:async                                   runZonedGuarded
package:bloc_test/src/bloc_test.dart 172:9   testBloc
package:bloc_test/src/bloc_test.dart 140:11  blocTest.<fn>
package:bloc_test/src/bloc_test.dart 139:26  blocTest.<fn>
Expected: [
            SettingsState:SettingsState(expandNavigation: false, gpsEnabled: false, celsiusEnabled: false, notificationsEnabled: false,breakingNewsNotifications: false, trendingNotifications: false, liveRemindersNotifications: false, sportsNotifications: false)
          ]
  Actual: []
   Which: at location [0] is [] which shorter than expected

SettingsCubit和SettingsState代码位于:Flutter BLoC Test failures

EN

回答 1

Stack Overflow用户

发布于 2021-08-05 19:02:52

如果您想测试您的Cubit的初始状态,您应该使用第一种方法(对您有效的方法)。

bloc_test文档中描述了同一测试的第二个版本(blocTest one)无法工作的原因:

expect是一个可选的Function,它返回一个Matcher,被测bloc期望在act执行后发出该act。

这意味着,您应该将所有状态更改放在expect方法中。但是现在,您只是创建了一个BLoC,之后没有执行任何操作,您的状态不会改变,因此实际的结果--一个预期状态改变的列表--是空的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68667461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档