我正在跟踪Jest的文档,但是我无法避免下面的错误。expect(dummyFunction).toHaveBeenNthCalledWith is not a function
除非我遗漏了什么,否则我非常肯定,我的dummyFunction
设置作为jest.fn()
是正确的。在测试中使用dummyFunction
之前,我甚至安慰了它的输出,这就是输出。
dummyFunction console.log输出
{ [Function: mockConstructor]
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockReturnValueOnce: [Function],
mockReturnValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
mockRestore: [Function] }
toHaveBeenCalledNthWith测试
const dummyFunction = jest.fn();
expect(dummyFunction).toHaveBeenCalledTimes(2); // pass
expect(dummyFunction).toHaveBeenNthCalledWith(1, { foo: 'bar' }); // error
expect(dummyFunction).toHaveBeenNthCalledWith(2, { please: 'work' });
提前谢谢你的帮助。
发布于 2019-02-06 07:25:17
toHaveBeenNthCalledWith
是在Jest
版本23.0.0中发布的,所以如果您使用的是较早版本的Jest
,您将看到该错误。
请注意,toHaveBeenNthCalledWith
只是spy.mock.calls[nth]
,所以如果您使用的是较早版本的Jest
,则只需执行以下操作:
const dummyFunction = jest.fn();
dummyFunction({ foo: 'bar' });
dummyFunction({ please: 'work' });
expect(dummyFunction).toHaveBeenCalledTimes(2); // pass
expect(dummyFunction.mock.calls[0]).toEqual([{ foo: 'bar' }]); // pass
expect(dummyFunction.mock.calls[1]).toEqual([{ please: 'work' }]); // pass
https://stackoverflow.com/questions/54540006
复制相似问题