簡體   English   中英

我怎樣才能得到在開玩笑模擬 function 中調用的 arguments?

[英]How can I get the arguments called in jest mock function?

我怎樣才能得到在開玩笑模擬 function 中調用的 arguments?

我想檢查作為參數傳遞的 object。

只需使用mockObject.calls 就我而言,我使用了:

const call = mockUpload.mock.calls[0][0]

這是有關mock屬性文檔

這是斷言傳遞的參數的簡單方法。

expect(mockedFunction).toHaveBeenCalledWith("param1","param2");

我更喜歡lastCalledWith()不是toHaveBeenCalledWith() 它們都是一樣的,但前者更短,幫助我減少閱讀代碼時的認知負擔。

expect(mockedFn).lastCalledWith('arg1', 'arg2')

您可以使用toHaveBeenCalledWith()連同expect.stringContainingexpect.arrayContaining()expect.objectContaining()

...
const { host } = new URL(url);
expect(mockedFunction).toHaveBeenCalledWith("param1", expect.stringContaining(`http://${host}...`);

我們可以在 mock 方法中添加一個 mockImplementation 並讓它返回 arguments 回用於評估。

暫無
暫無

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

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