【发布时间】:2019-04-15 00:46:10
【问题描述】:
我正在测试一个返回整数的实用函数,我正在尝试模拟调用它,但即使经过数小时的谷歌搜索,我也找不到正确的方法。
我也试过spyOn(),但似乎没有用。
身份验证.js
export function auth(username) {
AsyncStorage.getItem('@app:id').then((id) => {
if (id === username) {
return 1;
}
return 0;
});
}
Authentication.test.js
import 'react-native';
import React from 'react';
import renderer from 'react-test-renderer'; // Note: test renderer must be required after react-native.
import mockAxios from 'axios';
import mockAsyncStorage from '@react-native-community/async-storage';
import auth from '../App/Utils/Authorization';
test('should check whether the user whose username is entered as a paramter is the same as the user logged in the application', () => {
auth = jest.fn();
expect(auth).toHaveReturned();
expect(mockAsyncStorage.getItem).toHaveBeenCalledTimes(1);
expect(mockAsyncStorage.multiRemove).toHaveBeenCalledWith('@app:id');
});
我希望模拟调用 auth() 并成功测试,但每当运行 yarn test 时,我都会收到错误 "auth" is read-only 作为输出。
【问题讨论】:
标签: react-native jestjs