Test that tests a cubit that calls an emitting method in its constructor fails

66 Views Asked by At

What am I doing wrong here?

Error:

Expected: should emit an event that <Instance of 'AlbumListData'>
  Actual: <Instance of '_MapStream<AlbumListState, AlbumListState>'>
   Which: emitted • Instance of 'AlbumListData'

Cubit:

class AlbumListCubit extends Cubit<AlbumListState> {
  final AlbumListRepo albumListRepo;

  AlbumListCubit(this.albumListRepo) : super(AlbumListLoading()) {
    getAlbumList();
  }

  void getAlbumList() async {
    emit(AlbumListLoading());
    final result = await albumListRepo.fetch();
    result.fold(
      (failure) {
        log('getAlbumList Error: ${failure.message}');
        failure is RestApiError
            ? emit(const AlbumListError('Something went wrong!'))
            : emit(AlbumListError(failure.message));
      },
      (albumList) => emit(AlbumListData(albumList)),
    );
  }
 }

Test:

blocTest<AlbumListCubit, AlbumListState>(
      'getAlbumList emits AlbumListLoading and AlbumListData',
      setUp: () => when(() => mockRepo.fetch()).thenAnswer((_) async => Right(albumList)),
      build: () => AlbumListCubit(mockRepo),
      act: (bloc) {},
      expect: () =>  [
        AlbumListLoading(),
        AlbumListData(albumList),
      ],
    );
flutter doctor -v

[✓] Flutter (Channel stable, 3.10.4, on macOS 13.4 22F66 darwin-arm64 (Rosetta), locale en-LT) • Flutter version 3.10.4 on channel stable at /Users/mantasm1/fvm/versions/stable • Upstream repository https://github.com/flutter/flutter.git • Framework revision 682aa387cf (4 days ago), 2023-06-05 18:04:56 -0500 • Engine revision 2a3401c9bb • Dart version 3.0.3 • DevTools version 2.23.1

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/mantasm1/Library/Android/sdk • Platform android-33, build-tools 30.0.3 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14E300c • CocoaPods version 1.11.3

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.75.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.58.0

[✓] Connected device (2 available) • macOS (desktop) • macos • darwin-arm64 • macOS 13.4 22F66 darwin-arm64 (Rosetta) • Chrome (web) • chrome • web-javascript • Google Chrome 114.0.5735.106

[✓] Network resources • All expected network resources are available.

• No issues found!

0

There are 0 best solutions below