我想测试一个Bloc类,其中通过以下方式实现mapEventToState()。 当我针对我的对象的状态变化运行ExpectLater的测试时,出现以下问题:
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'>
请按照以下步骤检查我的代码:
集团类别-
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();
}
}
在这里我想测试这个Bloc。
Bloc测试类-
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')));
});
});
}