所以我尝试对一个块进行单元测试
我的测试很简单
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'>
所以它说它发射了初始状态,但实际上没有?
2条答案
按热度按时间z3yyvxxp1#
我们也有同样的错误。使用equatable修复了这个错误。
dgsult0t2#
我希望这个问题仍然具有现实意义。
实际上,没有必要从头开始写流和yield语句,就像新版本的bloc一样。但是当我们在测试中设置Later调用时,我们需要在期望结果的同时解析一个块流。所以,代码的解决方案是使用bloc.stream而不是bloc.state,它将块转换为块流并按顺序输出。请记住,流不包含初始/EmptyState(),但所有其他事件都按顺序处理,因此发出相同的事件。请参考以下代码片段以获得正确的实现,