我正在学习测试驱动开发,并试图通过遵循https://react-native-async-storage.github.io/async-storage/docs/advanced/jest/的指导来实现异步存储测试。我已经遵循了安装,目前正在尝试在我的第一个测试中模拟异步存储。
我创建了一个简单的测试。
import React from 'react';
import {render, fireEvent} from 'react-native-testing-library';
import UserWelcome from '../UserWelcome';
import AsyncStorage from '@react-native-async-storage/async-storage';
describe('UserWelcome', () => {
describe('User enters a name and stores in async local storage', () => {
const firstTimeUser = 'user001';
let getByTestId;
beforeEach(() => {
({getByTestId} = render(<UserWelcome />));
fireEvent.changeText(getByTestId('username'), firstTimeUser);
fireEvent.press(getByTestId('submitUsername'));
});
it('checks if Async Storage is used', async () => {
await asyncOperationOnAsyncStorage();
expect(AsyncStorage.getItem).toBeCalledWith('currentUser');
});
});
});然后我得到了错误ReferenceError: asyncOperationOnAsyncStorage没有定义,有没有人可以帮我一下,我应该从哪里得到asyncOperationOnAsyncStorage()函数。医生只是说
Each public method available from Async Storage is a mock function, that you can test for certain condition, for example, if .getItem has been called with a specific arguments:发布于 2020-12-08 00:28:30
我想你误解了文档。asyncOperationOnAsyncStorage只是文档中定义的一个示例方法,您必须将asyncOperationOnAsyncStorage替换为您有一些异步存储操作的方法。
因此,从本质上讲,asyncOperationOnAsyncStorage将是包含异步存储逻辑的方法。
asyncOperationOnAsyncStorage = () => {
await AsyncStorage.setItem('currentUser', value)
}您将使用Jest对其进行测试。
https://stackoverflow.com/questions/65185354
复制相似问题