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

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

使用 Jest 對復雜對象使用 Spies 和 Mock

使用 Jest 對復雜對象使用 Spies 和 Mock

慕蓋茨4494581 2022-12-22 15:51:23
我對使用 Jest 為當前未發(fā)現(xiàn)的 javaScript 代碼庫進行測試和編寫測試還很陌生。該代碼涵蓋了一些利基用例,因為它在頁面加載期間由瀏覽器有條件地注入和執(zhí)行。無論如何,我在模擬自定義對象時遇到了問題。這是有問題的功能:const setEnterpriseCookie = () => {        // Get the current page uri        let path = window.location.pathname;        // Matches all pages containing '/regex_expression'        if (path.match(/.*\/regex_expression.*/)) {            window.TOOL.cookie.setCookie(...args);        }    };據(jù)我了解,我需要模擬兩者window.location.pathname以返回一個字符串,并且我需要模擬window.TOOL.cookie.setCookie()為模擬函數(shù)。這是我的測試嘗試:var windowSpy;describe('Tests for the page-specific-methods.js file', () => {    beforeEach( () => {        windowSpy = jest.spyOn(global, 'window', 'get');    });    afterEach( () => {        windowSpy.mockRestore();    })    test('Test the page path detecting the enterprise string', () => {        windowSpy.mockImplementation( () => ({            location: {                pathname: '/enterprise/contact',            },            TOOL: {                cookie: {                    setCookie: jest.fn(),                },            },        }));        setEnterpriseCookie();                expect(window.TOOL.cookie.setCookie).toBeCalledTimes(1);        expect(window.TOOL.cookie.setCookie).toHaveBeenLastCalledWith(...args);    })});測試失敗,表示window.TOOL.cookie.setCookie被調(diào)用了 0 次。我深入研究了這個過程,發(fā)現(xiàn)它window.location.pathname按預期執(zhí)行,因此代碼進入了調(diào)用window.TOOL.cookie.setCookie. 我認為問題出在我如何模擬的某個地方window.TOOL.cookie.setCookie,但我一直沒能找到任何描述如何模擬如此之深的方法的幫助。先謝謝您的幫助!
查看完整描述

1 回答

?
慕萊塢森

TA貢獻1810條經(jīng)驗 獲得超4個贊

只需使用Object.defineProperty()直接在window對象上定義屬性。


例如


index.js:


const setEnterpriseCookie = (...args) => {

  let path = window.location.pathname;

  if (path.match(/.*\/enterprise.*/)) {

    window.TOOL.cookie.setCookie(...args);

  }

};


exports.setEnterpriseCookie = setEnterpriseCookie;

index.test.js:


const { setEnterpriseCookie } = require('./');


describe('63274598', () => {

  describe('Tests for the page-specific-methods.js file', () => {

    test('Test the page path detecting the enterprise string', () => {

      Object.defineProperty(window, 'location', {

        value: { pathname: '/enterprise/contact' },

      });

      Object.defineProperty(window, 'TOOL', {

        value: {

          cookie: {

            setCookie: jest.fn(),

          },

        },

      });

      setEnterpriseCookie('123');


      expect(window.TOOL.cookie.setCookie).toBeCalledTimes(1);

      expect(window.TOOL.cookie.setCookie).toHaveBeenLastCalledWith('123');

    });

  });

});

單元測試結(jié)果:


 PASS  stackoverflow/63274598/index.test.js (13.923s)

  63274598

    Tests for the page-specific-methods.js file

      ? Test the page path detecting the enterprise string (4ms)


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

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

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

All files |     100 |       50 |     100 |     100 |                   

 index.js |     100 |       50 |     100 |     100 | 3                 

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

Test Suites: 1 passed, 1 total

Tests:       1 passed, 1 total

Snapshots:   0 total

Time:        15.975s


查看完整回答
反對 回復 2022-12-22
  • 1 回答
  • 0 關(guān)注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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