[英]How can I get the arguments called in jest mock function?
我怎樣才能得到在開玩笑模擬 function 中調用的 arguments?
我想檢查作為參數傳遞的 object。
這是斷言傳遞的參數的簡單方法。
expect(mockedFunction).toHaveBeenCalledWith("param1","param2");
我更喜歡lastCalledWith()
不是toHaveBeenCalledWith()
。 它們都是一樣的,但前者更短,幫助我減少閱讀代碼時的認知負擔。
expect(mockedFn).lastCalledWith('arg1', 'arg2')
您可以使用toHaveBeenCalledWith()
連同expect.stringContaining
或expect.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.