簡體   English   中英

Jest - 使用spyOn函數時,確保不調用spied

[英]Jest - when using spyOn function ensure the spied one is not called

來自Jest注意: 注意:默認情況下,jest.spyOn也會調用spied方法。

在我的Angular組件中。

ngAfterViewInit(): void {
  this.offsetPopoverPosition();
}

在我的規格中:

it('ngAfterViewInit() method should call offsetPopoverPosition() method', () => {
    const mockListener = jest.spyOn(cmp, 'offsetPopoverPosition');
    const spy = mockListener.mockImplementation(() => {
      console.log('in the mock');
    });

    cmp.ngAfterViewInit();
    expect(spy).toHaveBeenCalled();
  });

簡單。 然而原始功能仍然被調用。 我檢查了Jest 23.x文檔: https //jestjs.io/docs/en/23.x/jest-object#jestspyonobject-methodname https://jestjs.io/docs/en/23.x/mock-function -API#mockfnmockimplementationfn

關於互聯網的幾個例子,但我不能阻止jest調用原始的offsetPopoverPosition()方法。

有任何想法嗎?

我正在交叉鏈接到Jest github問題,由於某種原因關閉而沒有解決它。

Jest spyOn()調用實際函數而不是模擬函數

根據我的經驗,問題是你正在重置原始模擬的意圖。 當你創建一個間諜時,它有自己的實現,通過用mockImplementation覆蓋它,我已經體驗過你正在描述的場景 - 相反,試試這個:

cmp.offsetPopoverPosition = jest.fn().mockImplementation(() => {
      console.log('in the mock');
    });
const mockListener = jest.spyOn(cmp, 'offsetPopoverPosition');
// ... do work
expect(mockListener).toHaveBeenCalled[Times,With]()

這也假設cmp是組件的一個實例,而不僅僅是它的定義參考

編輯:請注意,在您正在測試的組件內部模擬一個消息函數是一種誤導的單元測試方法。 而不是測試與sameComponent.method通信 - 測試鏈接方法在被測試組件之外使用的任何消息 - 使用簡短的問題內容,​​請忽略我給出的測試方法建議,如果它讀取茶葉並且與您的相關單元測試設計

我相信我在這里遇到同樣的問題,所以也許我們互相幫助。 我有3個文件

  • 一個叫dbManager.js
  • 另一個叫做mssqlPool.js
  • 然后是測試文件dbManager.test.js

測試的文件位於./src/db文件夾中

測試文件位於./test/db文件夾中 (基本上復制了src文件夾樹)。

我想模仿mssqlPool.js

module.exports = {
  mssqlPool: async config => {
    // actual connection with an await (in case it matters)
  }
}

這是DBManger,它是一個帶有構造函數的類:

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

module.exports = class DBManager {
  constructor(config) {}
}

async initPool() {
    if (global.DB_TYPE === 'mssql') {
      this.pool = await mssqlPool(this.config);
      return true;
    }}

const mssqlPool = require('../../../src/helpers/db/mssqlPool');
const DbManager = require('../../../src/helpers/db/dbManager');

let dbManager;

beforeEach(() => {
  const mockedConfig = {
    type: '<type: mssql/postgresql>',
    user: '<user>',
    password: '<mdp>',
    server: '<host>',
    database: '<db_name>',
    pool: {
      max: 10,
      min: 0,
      idleTimeoutMillis: 30000
    }
  };
  dbManager = new DbManager(mockedConfig);

  jest.spyOn(mssqlPool, 'mssqlPool').mockImplementation(() => Promise.resolve());
});

afterEach(() => {
  jest.restoreAllMocks();
});

describe('DbManager', () => {
  test('initPool', async () => {
    global.DB_TYPE = 'mssql';
    await dbManager.initPool();

    expect(mssqlPool.mssqlPool).toHaveBeenCalled();
  });});

這不起作用,測試失敗,因為它實際上嘗試使用mssqlPool函數並使用我的mockedConfig連接到數據庫,而我明確地模擬了函數

  jest.spyOn(mssqlPool, 'mssqlPool').mockImplementation(() => Promise.resolve());

我已經將我的mssqlPool文件從module.exports=config=>{}更改為module.exports={mssqlPool:config=>{}}以便能夠使用spyOn

暫無
暫無

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

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