我目前正在尝试对BLoC Flutter代码进行单元测试,但是由于我将其描述为“嵌套”流,因此测试失败。

这是测试代码:

test('fetch carbon events successfully', () async {
      final List<CarbonAction> actions = [];
      final List expected = [
        StateA(),
        StateB(),
        StateC()
      ];


      when(repo.getActions()).thenAnswer((_) => Stream.value(actions));

      expectLater(
        homeBloc.state,
        emitsInOrder(expected),
      );

      homeBloc.dispatch(FetchActionsEvent());
    });

失败并显示此错误:
Expected: should do the following in order:
          • emit an event that StateA:<StateA>
          • emit an event that StateB:<StateB>
          • emit an event that StateC:<StateC>
  Actual: <Instance of 'BehaviorSubject<State>'>
   Which: emitted • StateA
                  • StateB
                  • StateC
            which didn't emit an event that StateC:<StateC>

我相信这是因为StateC实际上来自使用async*yield*发出的流。在我的代码中,所有这些目前都在起作用。

最佳答案

因此,事实证明这只是一条非常无用的错误消息,而这仅仅是测试未通过。

我要确保对象的所有字段都完全匹配,尤其是在使用Equatable的情况下。

10-06 05:19