【发布时间】:2020-06-19 07:25:13
【问题描述】:
我正在尝试使用 Firestore 模拟器编写单元测试。
我已经使用以下命令启动了模拟器:
firebase emulators:start --only firestore
测试如下:
testWidgets('should retrieve correct item', (tester) async {
await Firestore.instance.settings(
host: "127.0.0.1:8080",
sslEnabled: false,
persistenceEnabled: false,
);
var ref = await Firestore.instance
.collection('books')
.add({ 'title': 'title'});
// Fetch item by id
var resp = await Firestore.instance
.collection('books')
.document(ref.documentID)
.get();
expect(resp, isNotNull);
});
问题是代码挂在.settings() 方法上,永远不会继续下一步。
有趣的事实是,当我停止模拟器时,测试的行为相同并挂在 .add() 方法上,这让我怀疑我这样做的方式是否正确。
【问题讨论】:
-
实际上小部件测试会模拟并阻止任何网络调用,无论是本地主机还是服务器。如果您必须根据 Firestore 结果测试小部件,请尝试模拟课程。否则,只是尝试使用。来自
package:test的test方法用于单元测试。 -
谢谢您提供这些信息。我不知道小部件测试阻止了任何网络访问。
标签: firebase flutter google-cloud-firestore firebase-cli flutter-test