I have a firestore database class, which I am trying to test. I have multiple objects which can be saved to different collections depending on the path provided. While testing the creation of an Item document and a Review object document - the Item object test passes and the Review object test fails, despite being almost identical tests. Here is some sample code:
///FirestoreService class - setData method
///I have an updated version of this method with a .catchError((e) => false);
///I have added it below, but I may be wrong on the syntax, I am adding this
///from memory as I am not at my regular workspace at the moment
Future<bool> setData(
{bool isAdd = false, String path, Map<String, dynamic> data}) async {
if (isAdd) {
await Firestore.instance.collection(path).add(data).catchError((e) => false);
print('$path: $data');
} else {
final reference = Firestore.instance.document(path);
print('$path: $data');
await reference.setData(data).catchError((e) => false);
}
return true;
}
///FirestoreDatabase class
/// Paths.items() = "items"; references collection 'items'
/// Paths.reviews() = "reviews"; references collection 'reviews'
final _service = FirestoreService.instance;
FirestoreService set(FirestoreService service) => _service = service;
@override
Future<bool> addItem(Item item) async => await _service.setData(
isAdd: true, path: Paths.items(), data: item.toMap());
@override
Future<bool> addReview(Review review) async => await _service.setData(
isAdd: true, path: Paths.reviews(), data: review.toMap());
And the tests:
class MockFirestoreService extends Mock implements FirestoreService {}
main() {
MockFirestoreService service;
FirestoreDatabase db;
Item item;
Review review;
setup() {
service = MockFirestoreService();
db = FirestoreDatabase();
db.set(service); //Use the mock firestore service
item = Item(id: 'abc', name: 'abc', description: 'abc test');
review = Review(id: 'def' rating: 5.0, text: 'review of abc', itemId: 'abc');
}
test('This test passes', () async {
when(service.setData(isAdd: true, path: Paths.items(), data: item.toMap())
.thenAnswer((_) => Future.value(true);
final bool result = await db.addItem(item);
expect(result, true);
});
test('This test fails', () async {
when(service.setData(isAdd: true, path: Paths.reviews(), data: review.toMap())
.thenAnswer((_) => Future.value(true);
final bool result = await db.addReview(review);
expect(result, true);
});
}
The first test adding a mock item passes, where as the second test adding a review fails, expecting a value of true, but returning null.
I have also tried expecting a completion:
expect(db.setReview(review), completion(expect(true)));
I am failing to understand why one test passes, yet the other fails. Any help, or improvements to my code is appreciated! Thanks!
I normally do like this and have no problem: Set the mock functions, then assign the mock object right after that. For example:
when(service.setData(isAdd: true, path: Paths.items(), data: item.toMap())
.thenAnswer((_) => Future.value(true);
db.set(service);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.