Bru*_*sma 68
只需使用mockObject.calls.在我的情况下我用过:
const call = mockUpload.mock.calls[0][0]
Run Code Online (Sandbox Code Playgroud)
Jbo*_*aga 26
您可以与或toHaveBeenCalledWith()一起使用expect.stringContainingexpect.arrayContaining()expect.objectContaining()
...
const { host } = new URL(url);
expect(mockedFunction).toHaveBeenCalledWith("param1", expect.stringContaining(`http://${host}...`);
Run Code Online (Sandbox Code Playgroud)
Sen*_* SP 13
这是断言所传递参数的简单方法。
expect(mockedFunction).toHaveBeenCalledWith("param1","param2");
Run Code Online (Sandbox Code Playgroud)
使用参数捕获器,如下所示:
let barArgCaptor;
const appendChildMock = jest
.spyOn(foo, "bar")
.mockImplementation(
(arg) => (barArgCaptor = arg)
);
Run Code Online (Sandbox Code Playgroud)
然后你就可以expect随心所欲地对俘虏的属性进行评价了:
expect(barArgCaptor.property1).toEqual("Fool of a Took!");
expect(barArgCaptor.property2).toEqual(true);
Run Code Online (Sandbox Code Playgroud)
我发现我经常想要验证对模拟函数的精确调用的确切参数;我的方法是:
let mockFn = jest.fn((/* ... */) => { /* ... */ });
// ... do some testing which triggers `mockFn` ...
expect(mockFn.mock.calls).toEqual([
// Ensure `mockFn` was called exactly 3 times:
// The 1st time with arguments "a" and 1,
[ 'a', 1 ],
// 2nd time with arguments "b" and 2,
[ 'b', 2 ],
// 3rd time with arguments "c" and 3
[ 'c', 3 ]
]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20786 次 |
| 最近记录: |