这是我要测试的课程:
//Request.js
import axios, {AxiosInstance} from 'axios';
import config from './config';
const axiosSingleton: AxiosInstance = axios.create({
baseURL: 'http://localhost:8080',
});
export default class Request {
public async get<$ResponseType = any>(url: string): Promise<void> {
const response = await axiosSingleton.get(url);
return response.data;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试通过创建测试文件进行测试时,我不确定如何模拟axios。我尝试了很多方法,包括-spyOn和自动嘲笑。但是它们似乎不起作用。这是测试文件的一个版本,我不明白为什么它不起作用
// Request.test.js
import axios from 'axios';
import Request from './Request';
interface ITestResponseDataType {
value: string
}
jest.mock('axios');
describe('Request Tests', () => {
it('should call axios get with the right relativeUrl', async () => {
const getMock = jest.fn(); …Run Code Online (Sandbox Code Playgroud)