Flutter Bloc测试-发出的未发出初始状态

时间:2020-10-28 04:25:40

标签: flutter dart

我真的很困惑如何使集团发出初始状态。 bloc.state发出最新状态。由于新的块库中没有@override initialState,因此将initialState传递给超级构造函数。但是,仍然集团不发出initialState,在这种情况下为InitialEmpty()。

number_trivia_bloc.dart

getItem()

number_trivia_bloc_test.dart

import 'package:clean_architecture/core/error/failure.dart';
import 'package:clean_architecture/core/utils/input_converter.dart';
import 'package:clean_architecture/features/number_trivia/domain/usecases/get_concrete_number_trivia_repository.dart';
import 'package:clean_architecture/features/number_trivia/domain/usecases/get_random_number_trivia.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'number_trivia_state.dart';
import 'number_trivia_event.dart';

const SERVER_FAILURE = 'Server failure';
const CACHE_FAILURE = 'Cache failure';
const INVALID_INPUT_FAILURE =
    'Invalid Input Failure - The input should not be a negative integer or zero';

class NumberTriviaBloc extends Bloc<NumberTriviaEvent, NumberTriviaState> {
  final GetConcreteNumberTrivia getConcreteNumberTrivia;
  final GetRandomNumberTrivia getRandomNumberTrivia;
  final InputConverter inputConverter;

  NumberTriviaBloc({
    // Changed the name of the constructor parameter (cannot use 'this.')
    @required GetConcreteNumberTrivia concrete,
    @required GetRandomNumberTrivia random,
    @required this.inputConverter,
    // Asserts are how you can make sure that a passed in argument is not null.
    // We omit this elsewhere for the sake of brevity.
  })  : assert(concrete != null),
        assert(random != null),
        assert(inputConverter != null),
        getConcreteNumberTrivia = concrete,
        getRandomNumberTrivia = random,
        super(Empty());

  @override
  Stream<NumberTriviaState> mapEventToState(
    NumberTriviaEvent event,
  ) async* {
    if (event is GetTriviaForConcreteNumber) {
      final inputEither =
          inputConverter.stringToUnsignedInteger(event.numberString);
      yield* inputEither.fold(
        (failure) async* {
          yield Error(message: INVALID_INPUT_FAILURE);
        },
        (integer) async* {
          yield Loading();
        },
      );
    }
  }
}

应该可以,但是我得到的只是这个错误向我尖叫。我尝试bloc.cast(),bloc.asBroadcastStream()。似乎什么都没有用,所以请有人能帮我解决这个问题。提前致谢。 Error

1 个答案:

答案 0 :(得分:0)

从flutter_bloc 6.0.0起有一些变化

您可以通过以下方式检查初始状态

test('initial state is correct', () {
  expect(bloc.state, Empty());
});

在这里提到:
Regression: initial state is not emited anymore