Skip to content

Instantly share code, notes, and snippets.

@techpotatoes
Last active April 18, 2020 09:33
Show Gist options
  • Save techpotatoes/ebbf25f4c9ca4e9d73fb08f672e3d4c4 to your computer and use it in GitHub Desktop.
Save techpotatoes/ebbf25f4c9ca4e9d73fb08f672e3d4c4 to your computer and use it in GitHub Desktop.
Flutter development series - part3
class MockLoyaltyCardBox extends Mock implements LoyaltyCardBox {}
class MockBox<T> extends Mock implements Box<T> {}
void main() {
final loyaltyCard1 = LoyaltyCard.fromParams('Card1','12345');
final loyaltyCard2 = LoyaltyCard.fromParams('Card2','54321');
final loyaltyCardList = [loyaltyCard1, loyaltyCard2];
group('Given a Loyalty cards repository', () {
final mockLoyaltyCardBox = MockLoyaltyCardBox();
final mockBox = MockBox<LoyaltyCard>();
final loyaltyCardRepository = LoyaltyCardRepository(mockLoyaltyCardBox);
when(mockLoyaltyCardBox.box).thenAnswer((_) async => Future.value(mockBox));
when(mockBox.values).thenReturn(loyaltyCardList);
test('should retrieve all LoyaltyCards', () async {
final result = await loyaltyCardRepository.getAll();
expect(result, loyaltyCardList);
});
test('should save loyalty card in the box', () async {
final newCard = LoyaltyCard.fromParams('CardNew','222333');
await loyaltyCardRepository.save(newCard);
verify(mockBox.add(newCard));
});
test('should delete loyalty card from the box', () async {
final existingCard = LoyaltyCard.fromParams('ExistingCard','777878');
await loyaltyCardRepository.delete(existingCard);
verify(mockBox.delete(existingCard.key));
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment