簡體   English   中英

試圖用玩笑制作一個簡單的 Axios 模擬

[英]Trying to make a simple Axios mock with jest

我試圖理解這個例子,所以我嘗試的第一件事是刪除他的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' ,但沒有axios.request()沒有baseURL

我在https://repl.it/@SandraSchlichti/jest-playground#index.js在線

索引.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)
    });
});

由於您實現你打電話axios.request與具有一個基本URL對象,它不符合你的說法。

因此,您可以斷言必須使用具有 baseURL 的對象調用它

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

或者調用該方法的對象必須具有以下兩個屬性:

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

工作示例

我會推薦你​​使用非對稱匹配器

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

有時您有一個很大的對象,不需要匹配確切的對象,您可以選擇要比較的對象。

另一個例子可能是:

expect(mock).toHaveBeenCalledWith(expect.objectContaining({
  postalCode: expect.any(Number)
}));

這里你的例子工作https://repl.it/join/ldayfmqz-yoandrycollazo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM