繁体   English   中英

如何开玩笑地模拟 AsyncStorage?

[英]How to mock AsyncStorage in jest?

我正在使用 jest 对 react-native 应用程序进行单元测试。 我在我的一个组件中使用了 AsyncStorage。

componentWillMount() {
    this.setState({ avatarSource: null });
    AsyncStorage.getItem('UserDetails', (err, result) => {
      var objData = JSON.parse(result);
      this.setState({ userdetail: objData });
      axios.get(SERVER_URL + '/image/' + objData.userId + '.jpg' + '?$' + Math.random())
          .then(response => {
              var obj = { uri: response.config.url };
              this.setState({ avatarSource: obj });
      });
    });
}

如何在我的测试文件中模拟 AsyncStorage 的“结果”?

测试 get 方法的最佳方法是:

describe('Verifying getData function returns the right value', () => {
    it('Function getData must return the correct value given date', async function () {
        await getData('SOME_KEY');
        expect(AsyncStorage.getItem).toBeCalledWith('SOME_KEY');
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM