簡體   English   中英

我可以使用 Jest 模擬具有特定 arguments 的函數嗎?

[英]Can I mock functions with specific arguments using Jest?

我想用 Jest 模擬一個 function,但前提是它是用特定的 arguments 調用的,例如:

function sum(x, y) {
  return x + y;
}
    
// mock sum(1, 1) to return 4
sum(1, 1) // returns 4 (mocked)
sum(1, 2) // returns 3 (not mocked)

Ruby 的 RSpec 庫中實現了類似的功能:

class Math
  def self.sum(x, y)
    return x + y
  end
end

allow(Math).to receive(:sum).with(1, 1).and_return(4)
Math.sum(1, 1) # returns 4 (mocked)
Math.sum(1, 2) # returns 3 (not mocked)

我在測試中想要實現的是更好的解耦,假設我想測試一個依賴於sum的 function :

function sum2(x) {
  return sum(x, 2);
}

// I don't want to depend on the sum implementation in my tests, 
// so I would like to mock sum(1, 2) to be "anything I want", 
// and so be able to test:

expect(sum2(1)).toBe("anything I want");

// If this test passes, I've the guarantee that sum2(x) is returning
// sum(x, 2), but I don't have to know what sum(x, 2) should return

我知道有一種方法可以通過執行以下操作來實現:

sum = jest.fn(function (x, y) {
  if (x === 1 && y === 2) {
    return "anything I want";
  } else {
    return sum(x, y);
  }
});

expect(sum2(1)).toBe("anything I want");

但是如果我們有一些糖 function 來簡化它會很好。

聽起來合理嗎? 我們在 Jest 中是否已經具備此功能?

感謝您的反饋意見。

我發現了我的一個同事最近寫的這個庫: jest-when

import { when } from 'jest-when';

const fn = jest.fn();
when(fn).calledWith(1).mockReturnValue('yay!');

const result = fn(1);
expect(result).toEqual('yay!');

這是圖書館: https ://github.com/timkindberg/jest-when

STeve Shary提到的jest-when可能是最好的選擇。

如果你不想安裝新的庫,當你不關心原來的功能時,這里有一個單行的解決方案:

sum.mockImplementation((x, y) => x === 1 && y === 2 && "my-return-value")

不,在 Jest 中還沒有辦法做到這一點。 您可以為此使用sinons 存根 來自文檔:

stub.withArgs(arg1[, arg2, ...]);

僅為提供的參數存根方法。 這對於在您的斷言中更具表現力很有用,您可以在其中使用相同的調用訪問間諜。 創建一個可以針對不同參數采取不同行動的存根也很有用。

"test should stub method differently based on arguments": function () {
    var callback = sinon.stub();
    callback.withArgs(42).returns(1);
    callback.withArgs(1).throws("TypeError");

    callback(); // No return value, no exception
    callback(42); // Returns 1
    callback(1); // Throws TypeError
}

我想我也需要一種方法來模擬參數,但對我來說,我可以通過簡單地知道調用的順序來解決我的問題。

取自開玩笑的文檔

const filterTestFn = jest.fn();

// Make the mock return `true` for the first call,
// and `false` for the second call
filterTestFn.mockReturnValueOnce(true).mockReturnValueOnce(false);

因此,在上面的示例中,只要您知道函數在第一次傳遞時應該返回 true,而在第二次傳遞時應該返回 false,那么您就可以開始了!

您可以使用:

const mockSum = jest.fn();

mockSum.mockImplementation((x, y) => {
  // Return whatever you want based on x and y...
});

這可能會有所幫助...

我有類似的東西,我使用不同的參數調用相同的方法,需要從 stubbed/mocked 調用返回不同的結果。 當我調用模擬服務時,我使用了一個帶有函數列表的變量,我將函數從隊列頂部取出並執行函數。 它需要了解您正在測試的執行順序,並且不能真正處理通過參數改變響應,但允許我開玩笑地繞過限制。

var mockedQueue = [];
mockedQueue.push(() => {return 'A';})
mockedQueue.push(() => {return 'B';})

service.invoke = jest.fn(()=>{
    serviceFunctionToCall = mockedQueue.shift();
    return serviceFunctionToCall();
})
import * as helper from "../helper";  //file where all functions are

jest.spyOn(helper, "function_name").mockImplementation((argument) => {

// This argument is the one passed to the function you are mocking

  if (argument === "something") {
    return "something"
  } else {
    return "something else"
  }
});

只需使用mockImplementation而不是mockReturnValue

jest.spyOn(data, 'get').mockImplementation(
  (arg) => {
    if (arg === 'your_desired_arg') {
      return 'mocked_value';
    }
    return data.get(arg);
  },
);

暫無
暫無

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

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