mardi 23 juillet 2019

Bloc Testing assertion problem with BehaviourSubject

I want to test a Bloc class where mapEventToState() is implemented in following way. When I am running my test for expectLater against the state change of the my Object I am getting below issue:

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

Please check my code as per below:

Bloc class-

class PersonBloc extends Bloc<BlocEvent, PersonListState> {
  List<Person> _list = [Person('Suresh'), Person('Gagan'), 
  Person('Neeraj')];
  var isValidName = true;

  get _getPersonList =>
  PersonListState(persons: _list, isPersonNameValid: isValidName);

  @override
  PersonListState get initialState => _getPersonList;

   @override
   Stream<PersonListState> mapEventToState(BlocEvent event) async* {
     if (event is AddPersonEvent) {
     _addNewPerson(event.person);
      yield _getPersonList;
    } else if (event is ValidatePersonNameEvent) {
      _validatePersonName(event.name);
     yield _getPersonList;
    }
  }

    _addNewPerson(Person person) {
      _list.add(person);
    }

    _validatePersonName(String name) {
     isValidName = name.isNotEmpty;
    }

 @override
 void dispose() {
    super.dispose();
 }
}

And here I want to test this Bloc.

Bloc Test class-

void main() {
  group('PersonBloc', () {
  PersonBloc bloc;

 setUp(() {
   bloc = PersonBloc();
 });

test('test initial state is 3 persons', () {
  expect(bloc.initialState.persons.length, 3);
});

test('test person increased after adding one', () {

  List<PersonListState> states = [
    PersonListState(persons: _oldList),
    PersonListState(persons: _newLst)
  ];

  expectLater(
    bloc.state,
    emitsInOrder(states),
  );

  bloc.dispatch(AddPersonEvent(Person('suresh')));
});
 });
}

Aucun commentaire:

Enregistrer un commentaire