【发布时间】:2021-02-23 11:56:12
【问题描述】:
我正在尝试理解this example,所以我尝试的第一件事是删除他的axiosConfig.js,所以这个例子看起来更像是我想要解决的当前案例。但是我收到此错误
- Expected
+ Received
Object {
+ "baseURL": "https://jsonplaceholder.typicode.com/albums",
"method": "get",
"url": "/3/photos?_limit=3",
},
Number of calls: 1
39 | const photos = await getPhotosByAlbumID(3);
40 | expect(axios.request).toHaveBeenCalled();
> 41 | expect(axios.request).toHaveBeenCalledWith({ method: 'get', url: '/3/photos?_limit=3' })
| ^
42 | expect(photos.length).toEqual(3);
43 | expect(photos[0].albumId).toEqual(3)
44 | });
问题
谁能弄清楚如何修复失败的测试?
如果我从getPhotosByAlbumId() 中删除baseURL: 'https://jsonplaceholder.typicode.com/albums',但没有baseURL 的axios.request() 没有意义。
我在https://repl.it/@SandraSchlichti/jest-playground#index.js有它在线
index.js
const axios = require('axios');
const getPhotosByAlbumId = async (id) => {
const result = await axios.request({
baseURL: 'https://jsonplaceholder.typicode.com/albums',
method: 'get',
url: `/${id}/photos?_limit=3`
});
const { data } = result;
return data;
};
module.exports = getPhotosByAlbumId;
index.spec.js
const axios = require('axios');
const getPhotosByAlbumID = require('./index');
jest.mock('axios', () => {
return {
baseURL: 'https://jsonplaceholder.typicode.com/albums',
request: jest.fn().mockResolvedValue({
data: [
{
albumId: 3,
id: 101,
title: 'incidunt alias vel enim',
url: 'https://via.placeholder.com/600/e743b',
thumbnailUrl: 'https://via.placeholder.com/150/e743b'
},
{
albumId: 3,
id: 102,
title: 'eaque iste corporis tempora vero distinctio consequuntur nisi nesciunt',
url: 'https://via.placeholder.com/600/a393af',
thumbnailUrl: 'https://via.placeholder.com/150/a393af'
},
{
albumId: 3,
id: 103,
title: 'et eius nisi in ut reprehenderit labore eum',
url: 'https://via.placeholder.com/600/35cedf',
thumbnailUrl: 'https://via.placeholder.com/150/35cedf'
}
]
})
}
})
describe('test getPhotosByAlbumID ', () => {
afterEach(() => jest.resetAllMocks());
it('fetches photos by album id', async () => {
const photos = await getPhotosByAlbumID(3);
expect(axios.request).toHaveBeenCalled();
expect(axios.request).toHaveBeenCalledWith({ method: 'get', url: '/3/photos?_limit=3' })
expect(photos.length).toEqual(3);
expect(photos[0].albumId).toEqual(3)
});
});
【问题讨论】:
标签: javascript node.js ecmascript-6 jestjs