Flutter块emmitInOrder不发出初始状态,但会发出

w51jfk4q  于 2023-03-13  发布在  Flutter
关注(0)|答案(2)|浏览(133)

所以我尝试对一个块进行单元测试
我的测试很简单

test("When loading patients, bloc should emmit [Loading], [OwnersLoaded]", (){
      //arrange
      var owners = [Owner(id: "TestId")];
      when (mockPatientsRepository.getOwnersForCurrentPractice()).thenAnswer((_)=>Future.value(owners));
      final List<PatientsState> expected = [patientsBloc.initialState, Loading(), OwnersLoaded(owners)];

      //assert later
      expectLater(patientsBloc, emitsInOrder(expected));

      //act
      useCase.getPatients();
    });

所有者不覆盖等于和散列
我的错误消息

Expected: should do the following in order:
          • emit an event that <Instance of 'InitialPatientsState'>
          • emit an event that <Instance of 'Loading'>
          • emit an event that <Instance of 'OwnersLoaded'>
  Actual: <Instance of 'PatientsBloc'>
   Which: emitted • Instance of 'InitialPatientsState'
                  • Instance of 'Loading'
                  • Instance of 'OwnersLoaded'
            which didn't emit an event that <Instance of 'InitialPatientsState'>

所以它说它发射了初始状态,但实际上没有?

z3yyvxxp

z3yyvxxp1#

我们也有同样的错误。使用equatable修复了这个错误。

dgsult0t

dgsult0t2#

我希望这个问题仍然具有现实意义。
实际上,没有必要从头开始写流和yield语句,就像新版本的bloc一样。但是当我们在测试中设置Later调用时,我们需要在期望结果的同时解析一个块流。所以,代码的解决方案是使用bloc.stream而不是bloc.state,它将块转换为块流并按顺序输出。请记住,流不包含初始/EmptyState(),但所有其他事件都按顺序处理,因此发出相同的事件。请参考以下代码片段以获得正确的实现,

This is the bloc file that contains the bloc logic as per the latest versions.

///<inputs an event, outputs the state> => <NumberTriviaEvent, 
NumberTriviaState>
class NumberTriviaBloc extends Bloc<NumberTriviaEvent, NumberTriviaState> {
  final GetConcreteNumberTrivia concreteNumberTrivia;
  final GetRandomNumberTrivia randomNumberTrivia;
  final InputConverter inputConverter;

  NumberTriviaBloc(
      {required this.concreteNumberTrivia,
      required this.randomNumberTrivia,
      required this.inputConverter}) : super(EmptyState()) {
    on<NumberTriviaEvent>((event, emit) async{
        if(event is GetTriviaForConcreteNumber){
          final input = inputConverter.stringToUnsignedInteger(event.numberString);
          input.fold((lFailure){
             emit( const ErrorState(errorMessage: INVALID_INPUT_FAILURE_MESSAGE));
          }, (rSuccess) {
             emit(const LoadedState(numberTrivia: NumberTrivia(text: 'sample test', number: 1)));
          });
        }
    });
  }
}

This is the test file that tests the output over the block that is written above.

    test('should emit [Error] when theres an invalid input', () {
      //  arrange
      when(inputConverter.stringToUnsignedInteger('abc'))
          .thenAnswer((realInvocation) => Left(InvalidInputFailure()));

      //  assert Later
      /// As we will be checking if the emit contains the expected result, we will use expectLater() method in test
      /// which puts the particular test on hold until the data is been emitted(upper limit 30 seconds) & then applies the checks
      expectLater(
        bloc.stream,
        emitsInOrder([
          const ErrorState(errorMessage: INVALID_INPUT_FAILURE_MESSAGE)
          ]));

      //  act
      bloc.add(const GetTriviaForConcreteNumber(numberString: 'abc'));
    });
  });

Learning Source: Reso Coders

相关问题