Флаттер не генерирует событие, которое Ошибка: <Error> - PullRequest
0 голосов
/ 25 апреля 2020

У меня есть blo c, который должен выдавать NoData, Loading, Loaded, Error. Это должно быть разработкой, управляемой тестами с Blo c. Я хочу общаться с Firebase, чтобы получить элементы списка. Тест показывает следующее предупреждение:

package: test_api OutlookLater package: flutter_test / src / widget_tester.dart 271: 10 allowLater test / features / list_items_data / presentation / bloc / list_items_bloc_test.dart 102: 7 main .. dart: asyn c _AsyncAwaitCompleter.start test / features / list_items_data / presentation / bloc / list_items_bloc_test.dart 92: 5 основных ..

Expected: should do the following in order:
          • emit an event that NoData:<NoData>
          • emit an event that Loading:<Loading>
          • emit an event that Error:<Error>
  Actual: <Instance of 'ListItemsBloc'>
   Which: emitted • NoData
                  • Loading
                  • Error
            which didn't emit an event that Error:<Error>
test(
    'should emit [Loading, Error] when getting data fails',
    () async {
      // arrange
      when(mockGetListItems(any))
          .thenAnswer((_) async => Left(ServerFailure()));
      // assert later
      final expected = [
        NoData(),
        Loading(),
        Error(message: ListItemsBloc.serverFailureMessage),
      ];
      expectLater(bloc, emitsInOrder(expected));
      // act
      bloc.add(GetDataListForItems());
    },
  );

  test(
    'should emit [Loading, Error] with a proper message for the error when getting data fails',
    () async {
      // arrange
      when(mockGetListItems(any))
          .thenAnswer((_) async => Left(CacheFailure()));
      // assert later
      final expected = [
        NoData(),
        Loading(),
        Error(message: ListItemsBloc.cacheFailureMessage),
      ];
      expectLater(bloc, emitsInOrder(expected));
      // act
      bloc.add(GetDataListForItems());
    },
  );
class NoData extends ListItemsState {}

class Loading extends ListItemsState {}

class Loaded extends ListItemsState {
  final ListItemsData itemDataList;
  Loaded({@required this.itemDataList});
  @override
  List<Object> get props => [itemDataList];
}

class Error extends ListItemsState {
  final String message;
  Error({@required this.message});
  @override
  List<Object> get props => [message];
}
class ListItemsBloc extends Bloc<ListItemsEvent, ListItemsState> {
  static const String serverFailureMessage = 'Server Failure';
  static const String cacheFailureMessage= 'Cache Failure';

  final GetItemListData getItemDataList;
  ListItemsBloc({
    @required GetItemListData getItemDataList,
  })  : assert(getItemDataList != null),
        getItemDataList = getItemDataList;

  @override
  ListItemsState get initialState => NoData();

  @override
  Stream<ListItemsState> mapEventToState(
      ListItemsEvent event,
      ) async* {
    if (event is GetDataListForItems) {
      yield Loading();
      final failureOrNot = await getItemDataList(NoParams());
      yield* _eitherLoadedOrErrorState(failureOrNot);
    }
  }
}

   Stream<ListItemsState> _eitherLoadedOrErrorState(
    Either<Failure, ListItemsData> either,
    ) async* {
  yield either.fold(
        (failure) => Error(message: _mapFailureToMessage(failure)),
        (itemDataList) => Loaded(itemDataList: itemDataList),
  );
}

  String _mapFailureToMessage(Failure failure) {
  switch (failure.runtimeType) {
    case ServerFailure:
      return 'Server Error';
    case CacheFailure:
      return 'Cache Error';
    default:
      return 'Unexpected Error';
  }
}
...