第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在玩笑中正確使用 axios.get.mockResolvedValue 進(jìn)行異步調(diào)用

如何在玩笑中正確使用 axios.get.mockResolvedValue 進(jìn)行異步調(diào)用

慕田峪4524236 2021-11-12 14:21:25
我想用 Jest 模擬異步函數(shù)的 catch 塊中的返回值這是我正在為其編寫單元測(cè)試的函數(shù):  try {    make some axios request    }    return users;  } catch (err) {    return new Map();  }};    it('should catch error when query is unsuccessful', async () => {      axios.get.mockRejectedValue(new Map());      const res = getUserDataByIds('someUserIds');      await expect(res).rejects.toThrow();    });我從 Jest 收到錯(cuò)誤消息: expect(received).rejects.toEqual() Received promise resolved instead of rejected Resolved to value: Map {}我希望測(cè)試應(yīng)該通過(guò),因?yàn)槲以诔靶σ粋€(gè)被拒絕的值。
查看完整描述

1 回答

?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

您可以創(chuàng)建一個(gè)模擬函數(shù)來(lái)替換axios.get()方法。


index.ts:


import axios from 'axios';


export async function getUserDataByIds(ids: string[]) {

  try {

    const users = await axios.get('/users');

    return users;

  } catch (err) {

    return new Map();

  }

}

index.spec.ts:


import { getUserDataByIds } from './';

import axios from 'axios';


describe('getUserDataByIds', () => {

  it('should return empty Map when axios.get failed', async () => {

    const getError = new Error('network error');

    axios.get = jest.fn().mockRejectedValue(getError);

    const actualValue = await getUserDataByIds(['1']);

    expect(actualValue).toEqual(new Map());

    expect(axios.get).toBeCalledWith('/users');

  });


  it('should return users', async () => {

    const mockedUsers = [{ userId: 1 }];

    axios.get = jest.fn().mockResolvedValue(mockedUsers);

    const actualValue = await getUserDataByIds(['1']);

    expect(actualValue).toEqual(mockedUsers);

    expect(axios.get).toBeCalledWith('/users');

  });

});

100% 覆蓋率的單元測(cè)試結(jié)果:


 PASS  src/stackoverflow/58273544/index.spec.ts

  getUserDataByIds

    ? should return empty Map when axios.get failed (12ms)

    ? should return users (4ms)


----------|----------|----------|----------|----------|-------------------|

File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |

----------|----------|----------|----------|----------|-------------------|

All files |      100 |      100 |      100 |      100 |                   |

 index.ts |      100 |      100 |      100 |      100 |                   |

----------|----------|----------|----------|----------|-------------------|

Test Suites: 1 passed, 1 total

Tests:       2 passed, 2 total

Snapshots:   0 total

Time:        5.597s, estimated 7s

源代碼:https : //github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/58273544


查看完整回答
反對(duì) 回復(fù) 2021-11-12
  • 1 回答
  • 0 關(guān)注
  • 362 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)