问题描述
我正在使用React Native构建一个应用程序.我想尽量减少与数据库通信的频率,因此我大量使用了AsyncStorage.虽然在DB和AsyncStorage之间的转换中有很多错误的余地.因此,我想通过对它运行自动化测试来确保AsyncStorage拥有我相信的数据.令人惊讶的是,我还没有找到有关如何在线进行操作的任何信息.我自己尝试做的尝试还没有成功.
I'm building an app with React Native. I want to minimize how often I communicate to the database, so I make heavy use of AsyncStorage. There's a lot of room for bugs in the translation between DB and AsyncStorage though. Therefore, I want to make sure that AsyncStorage has the data I believe it does by running automated tests against it. Surprisingly, I haven't found any information on how to do that online. My attempts to do it on my own haven't worked out.
使用笑话:
it("can read asyncstorage", () => {
return AsyncStorage.getItem('foo').then(foo => {
expect(foo).not.toBe("");
}); });
此方法失败,并显示错误:
This method failed with an error:
TypeError: RCTAsyncStorage.multiGet is not a function
删除返回值将导致返回值立即运行,而无需等待该值并且不正确地通过了测试.
Removing the return will cause it to run instantly without waiting for the value and improperly pass the test.
当我尝试使用await关键字对其进行测试时,我遇到了完全相同的错误:
I got hit with the exact same error when I tried to test it using the await keyword:
it('can read asyncstorage', async () => {
this.foo = "";
await AsyncStorage.getItem('foo').then(foo => {
this.foo = foo;
});
expect(foo).not.toBe(""); });
关于如何针对AsyncStorage中的值成功运行断言的任何建议?我希望继续使用Jest,但如果只能通过一些替代测试库来完成,那么我可以接受.
Any suggestions on how to successfully run assertions against the values in AsyncStorage? I'd prefer to continue using Jest but if it can only be done with some alternate testing library I'm open to that.
推荐答案
我的原始答案只是指出react-native-simple-store的作者如何处理模拟.我用自己的模拟更新了答案,该模拟消除了Jason的硬编码模拟响应.
My original answer just pointed at how the author of react-native-simple-store had dealt with the mocking. I've updated my answer with my own mocking that removes Jason's hard-coded mock responses.
Jason Merino 在 https://github.com/jasonmerino/react-native-simple -store/blob/master/测试/index-test.js#L31-L64
Jason Merino has a nice simple approach to this in https://github.com/jasonmerino/react-native-simple-store/blob/master/tests/index-test.js#L31-L64
jest.mock('react-native', () => ({
AsyncStorage: {
setItem: jest.fn(() => {
return new Promise((resolve, reject) => {
resolve(null);
});
}),
multiSet: jest.fn(() => {
return new Promise((resolve, reject) => {
resolve(null);
});
}),
getItem: jest.fn(() => {
return new Promise((resolve, reject) => {
resolve(JSON.stringify(getTestData()));
});
}),
multiGet: jest.fn(() => {
return new Promise((resolve, reject) => {
resolve(multiGetTestData());
});
}),
removeItem: jest.fn(() => {
return new Promise((resolve, reject) => {
resolve(null);
});
}),
getAllKeys: jest.fn(() => {
return new Promise((resolve) => {
resolve(['one', 'two', 'three']);
});
})
}
}));
我自己的模拟:
const items = {};
jest.mock('react-native', () => ({
AsyncStorage: {
setItem: jest.fn((item, value) => {
return new Promise((resolve, reject) => {
items[item] = value;
resolve(value);
});
}),
multiSet: jest.fn((item, value) => {
return new Promise((resolve, reject) => {
items[item] = value;
resolve(value);
});
}),
getItem: jest.fn((item, value) => {
return new Promise((resolve, reject) => {
resolve(items[item]);
});
}),
multiGet: jest.fn((item) => {
return new Promise((resolve, reject) => {
resolve(items[item]);
});
}),
removeItem: jest.fn((item) => {
return new Promise((resolve, reject) => {
resolve(delete items[item]);
});
}),
getAllKeys: jest.fn((items) => {
return new Promise((resolve) => {
resolve(items.keys());
});
})
}
}));
这篇关于如何使用Jest测试异步存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!