【问题标题】:Flutter blocTest returning empty 'actual' arrayFlutter blocTest 返回空的“实际”数组
【发布时间】:2021-10-25 11:35:27
【问题描述】:

所以我有一个简单的 Cubit 类,如下所示: 需要根据列表的 scrollValue 来改变 isClosed 的值,所以如果用户向上滚动,一些小部件将会关闭。

const SCROLL_CLOSE_VALUE = 50;

class CloseContainerCubit extends Cubit<CloseContainerState> {
  CloseContainerCubit()
      : super(CloseContainerState(
          isClosed: false,
          scrollValue: .0,
        ));

  void updateCloseContainer(scroll) =>
      emit(CloseContainerState(isClosed: state.isClosed, scrollValue: scroll));

  void handleClosingContainer() {
    bool isClosedEmitValue = state.scrollValue > SCROLL_CLOSE_VALUE;

    emit(CloseContainerState(
        isClosed: isClosedEmitValue, scrollValue: state.scrollValue));
  }
}

状态类如下所示:

class CloseContainerState extends Equatable {
  final bool isClosed;
  final double scrollValue;

  CloseContainerState({required this.isClosed, required this.scrollValue});

  @override
  List<Object?> get props => [isClosed, scrollValue];
}

并尝试为它创建测试:


void main() {
  group('CloseContainerCubit', () {
    late CloseContainerCubit cubit;

    setUp(() {
      cubit = CloseContainerCubit();
    });

    tearDown(() {
      cubit.close();
    });

    test(
        'the initial state for the CloseContainerCubit is that cubit is not closed and scrollValue is 0)',
        () {
      expect(
          cubit.state, CloseContainerState(isClosed: false, scrollValue: .0));
    });

    blocTest<CloseContainerCubit, CloseContainerState>(
      'the cubit should emit a CloseContainerCubit(isClosed: false) '
      'when cubit.updateCloseContainer(scroll) is called'
      'and scroll < SCROLL_CLOSE_VALUE',
      build: () => cubit,
      act: (bloc) {
        bloc.updateCloseContainer(SCROLL_CLOSE_VALUE - 1);
        bloc.handleClosingContainer();
      },
      expect: () => <CloseContainerState>[
        CloseContainerState(
            isClosed: false, scrollValue: cubit.state.scrollValue)
      ],
    );
  });
}

第一个测试通过就好了,但是“blocTest”给我一个错误,“实际”是 []:

package:test_api                             expect
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: [CloseContainerState:CloseContainerState(false, 0.0)]
  Actual: []
   Which: at location [0] is [] which shorter than expected

我在这里做错了什么?为什么它总是返回一个空数组而不仅仅是一些状态?

【问题讨论】:

    标签: flutter unit-testing testing bloc bloc-test


    【解决方案1】:

    您的 updateCloseContainer 函数中缺少滚动参数的类型。

    void updateCloseContainer(double scroll) {
        emit(CloseContainerState(isClosed: state.isClosed, scrollValue: scroll));
    }
    

    【讨论】:

      猜你喜欢
      • 2022-08-16
      • 2019-04-15
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 2022-01-23
      • 2021-12-07
      相关资源
      最近更新 更多