试图用玩笑制作一个简单的Axios模拟 [英] Trying to make a simple Axios mock with jest

查看:51
本文介绍了试图用玩笑制作一个简单的Axios模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解此示例,所以我要尝试的第一件事就是删除他的axiosConfig.js,因此该示例看起来更像是我想解决的当前情况.但是我得到这个错误

I am trying to understand this example, so the first thing I am trying is removing his axiosConfig.js, so the example looks more like the current case I would like to solve. However I get this error

- 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 |     });

问题

任何人都可以弄清楚如何解决失败的测试吗?

Question

Can anyone figure out how to fix the failed test?

如果我从getPhotosByAlbumId()中删除了baseURL: 'https://jsonplaceholder.typicode.com/albums',但是没有baseURLaxios.request()没有意义.

If I remove baseURL: 'https://jsonplaceholder.typicode.com/albums' from getPhotosByAlbumId(), but it doesn't make sense to have axios.request() without a baseURL.

我可以通过 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)
    });
});

推荐答案

由于在您的实现中,您正在使用具有baseURL的对象调用axios.request,所以它与您的断言不匹配.

Since in your implementation you're calling axios.request with an object that has baseURL, it doesn't matches your assertion.

因此,您可以断言必须使用具有baseURL的对象来调用它

So you can either assert that it has to be called with an object that has baseURL

expect(axios.request).toHaveBeenCalledWith({
    baseURL: "https://jsonplaceholder.typicode.com/albums",
    method: "get",
    url: "/3/photos?_limit=3",
});

或者调用该方法的对象必须具有以下两个属性:

or that the object the method has been called with has to have these two properties:

expect(axios.request).toHaveBeenCalledWith(
    expect.objectContaining({ method: "get", url: "/3/photos?_limit=3" })
);

工作示例

这篇关于试图用玩笑制作一个简单的Axios模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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