0

所以我有一个简单的 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

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

4

1 回答 1

0

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

void updateCloseContainer(double scroll) {
    emit(CloseContainerState(isClosed: state.isClosed, scrollValue: scroll));
}
于 2021-08-31T05:01:58.013 回答