Skip to content

Instantly share code, notes, and snippets.

@techpotatoes
Created April 18, 2020 14:47
Show Gist options
  • Save techpotatoes/8cce31bcccd2401293e613d9c5f77da5 to your computer and use it in GitHub Desktop.
Save techpotatoes/8cce31bcccd2401293e613d9c5f77da5 to your computer and use it in GitHub Desktop.
Flutter development series - part4
class MockLoyaltyCardRepository extends Mock implements LoyaltyCardRepository {}
class UnhandledEvent extends LoyaltyEvent {}
void main() {
final loyaltyCard1 = LoyaltyCard.fromParams('Card1','12345');
group('Given a Loyalty cards BLoC', () {
final mockLoyaltyCardRepository = MockLoyaltyCardRepository();
test('should return error state when the event is not handled', () async {
final loyaltyBloc = LoyaltyBloc(loyaltyCardRepository: mockLoyaltyCardRepository);
loyaltyBloc.add(UnhandledEvent());
expectLater(
loyaltyBloc,
emitsInOrder([LoyaltyEmpty(), LoyaltyError()])
);
});
test('on Fetch should return loaded state when there are more than 0 cards', () async {
final loyaltyBloc = LoyaltyBloc(loyaltyCardRepository: mockLoyaltyCardRepository);
when(mockLoyaltyCardRepository.getAll()).thenAnswer((_) async => Future.value([loyaltyCard1]));
loyaltyBloc.add(Fetch());
expectLater(
loyaltyBloc,
emitsInOrder([LoyaltyEmpty(), LoyaltyLoading(), LoyaltyLoaded(loyaltyCards: [loyaltyCard1])])
);
});
test('on Fetch should return empty state when there are 0 cards', () async {
final loyaltyBloc = LoyaltyBloc(loyaltyCardRepository: mockLoyaltyCardRepository);
when(mockLoyaltyCardRepository.getAll()).thenAnswer((_) async => Future.value([]));
loyaltyBloc.add(Fetch());
expectLater(
loyaltyBloc,
emitsInOrder([LoyaltyEmpty(), LoyaltyLoading(), LoyaltyEmpty()])
);
});
test('on Fetch should return error state when there is an exception', () async {
final loyaltyBloc = LoyaltyBloc(loyaltyCardRepository: mockLoyaltyCardRepository);
when(mockLoyaltyCardRepository.getAll()).thenThrow(Exception());
loyaltyBloc.add(Fetch());
expectLater(
loyaltyBloc,
emitsInOrder([LoyaltyEmpty(), LoyaltyLoading(), LoyaltyError()])
);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment