[英]Flutter Unit Testing: Some tests pass, while other similar tests fail
I have a firestore database class, which I am trying to test.我有一个 firestore 数据库类,我正在尝试测试它。 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.在测试 Item 文档和 Review 对象文档的创建时 - Item 对象测试通过而 Review 对象测试失败,尽管它们是几乎相同的测试。 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.添加模拟项目的第一个测试通过,而添加评论的第二个测试失败,期望值为 true,但返回 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.