如何使用 Jest 测试异步存储? [英] How to test Async Storage with Jest?

查看:27
本文介绍了如何使用 Jest 测试异步存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 Merinohttps://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 测试异步存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆