Unit testing 颤振单元测试:一些测试通过,而其他类似测试失败
我有一个firestore数据库类,我正在尝试测试它。我有多个对象,可以根据提供的路径保存到不同的集合。在测试项目文档和评审对象文档的创建时,项目对象测试通过,评审对象测试失败,尽管是几乎相同的测试。以下是一些示例代码:Unit testing 颤振单元测试:一些测试通过,而其他类似测试失败,unit-testing,flutter,google-cloud-firestore,Unit Testing,Flutter,Google Cloud Firestore,我有一个firestore数据库类,我正在尝试测试它。我有多个对象,可以根据提供的路径保存到不同的集合。在测试项目文档和评审对象文档的创建时,项目对象测试通过,评审对象测试失败,尽管是几乎相同的测试。以下是一些示例代码: ///FirestoreService class - setData method ///I have an updated version of this method with a .catchError((e) => false); ///I have added
///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;
添加模拟项的第一个测试通过,其中添加审阅的第二个测试失败,预期值为true,但返回null
我也尝试过期待完成:
expect(db.setReview(review), completion(expect(true)));
我不明白为什么一个测试通过了,而另一个却失败了。感谢您对我的代码的任何帮助或改进!谢谢 我通常是这样做的,没有问题:设置mock函数,然后分配mock对象。例如:
when(service.setData(isAdd: true, path: Paths.items(), data: item.toMap())
.thenAnswer((_) => Future.value(true);
db.set(service);
不幸的是,这对我来说没有任何改变。我也有同样的失败原因
expect(db.setReview(review), completion(expect(true)));
when(service.setData(isAdd: true, path: Paths.items(), data: item.toMap())
.thenAnswer((_) => Future.value(true);
db.set(service);