【发布时间】:2017-04-18 13:34:54
【问题描述】:
我正在使用 React Native 构建一个应用程序。我想尽量减少与数据库通信的频率,所以我大量使用 AsyncStorage。但是,在 DB 和 AsyncStorage 之间的转换中存在很大的错误空间。因此,我想通过对它运行自动化测试来确保 AsyncStorage 拥有我相信的数据。令人惊讶的是,我还没有找到任何有关如何在线执行此操作的信息。我自己尝试做这件事并没有成功。
使用 Jest:
it("can read asyncstorage", () => {
return AsyncStorage.getItem('foo').then(foo => {
expect(foo).not.toBe("");
}); });
此方法失败并出现错误:
TypeError: RCTAsyncStorage.multiGet is not a function
删除 return 将导致它立即运行而无需等待 value 并错误地通过测试。
当我尝试使用 await 关键字对其进行测试时,遇到了完全相同的错误:
it('can read asyncstorage', async () => {
this.foo = "";
await AsyncStorage.getItem('foo').then(foo => {
this.foo = foo;
});
expect(foo).not.toBe(""); });
关于如何针对 AsyncStorage 中的值成功运行断言有什么建议吗?我更愿意继续使用 Jest,但如果它只能通过一些备用测试库来完成,我对此持开放态度。
【问题讨论】:
标签: reactjs asynchronous react-native jestjs asyncstorage