Search code examples
flutterunit-testingtestingblocbloc-test

Flutter blocTest returning empty 'actual' array


So i have a simple Cubit class that looks like this: It is ment to change the isClosed value based on the scrollValue of the list, so if user scrolls up, some widget is going to close.

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));
  }
}

state class looks like this:

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

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

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

And tried to create tests for it:


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)
      ],
    );
  });
}

First test passes just fine, but the "blocTest" one gives me an error and the "actual" is []:

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

What do I do wrong here? And why it always returns an empty array instead of just some state?


Solution

  • The type of the scroll parameter is missing in your updateCloseContainer function.

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