亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我可以監視對象而不是對象方法嗎?

我可以監視對象而不是對象方法嗎?

交互式愛情 2023-11-12 15:22:14
我知道當我引用了一個函數作為方法的對象時,我可以使用像下面這樣的間諜來跟蹤函數被調用的次數。jest.spyOn(myObj, 'myFunc')但是,當我引用了我想要使用的函數時,我該怎么辦?jest.spyOn(myFunc)不起作用為了澄清一下,我想使用該函數的實際實現。我只是想知道它被調用了多少次以及使用了哪些參數。當我嘗試在普通函數上查看這些內容時,我得到:  expect(received).toHaveBeenCalledWith(...expected)Matcher error: received value must be a mock or spy function這是(大部分)實際測試:  it('should set createScrollContextImmediately when result is above the maximum return limit', async () => {    // Arrange    ...    // Act    await fetch(imageID, { size: maxSizePerScroll }, ids, { createScrollContextImmediately: false });    // Assert    expect(fetch).toHaveBeenCalledWith(imageID, { size: maxSizePerScroll }, ids, { createScrollContextImmediately: false });    expect(fetch).toHaveBeenCalledWith(imageID, { size: maxSizePerScroll }, ids, { createScrollContextImmediately: true });    expect(fetch).toHaveBeenCalledTimes(2);  });
查看完整描述

1 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

選項 1.您可以使用真實的實現來模擬該fetch函數。jest.mock并且,我們可以在實現中添加一個間諜。你可以為那個間諜做出斷言。


例如


fetch.ts:


export async function fetch(name) {

  return 'real implementation';

}

fetch.test.ts:


import { fetch } from './fetch';


const fetchSpy = jest.fn();


jest.mock('./fetch', () => {

  const { fetch } = jest.requireActual('./fetch');

  const fetchWithSpy = jest.fn().mockImplementation((...args) => {

    fetchSpy(...args);

    return fetch(...args);

  });

  return {

    fetch: fetchWithSpy,

  };

});


describe('65266282', () => {

  it('should set createScrollContextImmediately when result is above the maximum return limit', async () => {

    const actual = await fetch('teresa teng');

    expect(actual).toBe('real implementation');

    expect(fetchSpy).toBeCalledWith('teresa teng');

    expect(fetchSpy).toHaveBeenCalledTimes(1);

  });

});

測試結果:


PASS  examples/65266282/fetch.test.ts

  65266282

    ? should set createScrollContextImmediately when result is above the maximum return limit (8 ms)


Test Suites: 1 passed, 1 total

Tests:       1 passed, 1 total

Snapshots:   0 total

Time:        4.216 s

選項 2.您可以使用Proxy為您的fetch函數創建代理。


Proxy 對象使您能夠為另一個對象創建代理,該代理可以攔截并重新定義該對象的基本操作。


fetch-v2.test.ts:


import { fetch } from './fetch';


const fetchSpy = jest.fn();

const fetchProxy = new Proxy(fetch, {

  apply: (target, thisArg, argumentsList) => {

    fetchSpy.apply(thisArg, argumentsList);

    return target.apply(thisArg, argumentsList);

  },

});


describe('65266282', () => {

  it('should set createScrollContextImmediately when result is above the maximum return limit', async () => {

    const actual1 = await fetchProxy('teresa teng');

    expect(actual1).toBe('real implementation');

    const actual2 = await fetchProxy('best singer');

    expect(actual2).toBe('real implementation');

    expect(fetchSpy).toBeCalledWith('teresa teng');

    expect(fetchSpy).toBeCalledWith('best singer');

    expect(fetchSpy).toHaveBeenCalledTimes(2);

  });

});

測試結果:


 PASS  examples/65266282/fetch-v2.test.ts

  65266282

    ? should set createScrollContextImmediately when result is above the maximum return limit (3 ms)


Test Suites: 1 passed, 1 total

Tests:       1 passed, 1 total

Snapshots:   0 total

Time:        4.484 s

這兩種方法本質上是相同的。核心思想是使用代理、攔截器、高階函數


查看完整回答
反對 回復 2023-11-12
  • 1 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號