我想测试一个函数,它只返回一个带有一些值的函数和一个匿名函数作为参数。如何在玩笑中使用 toHaveBeenCalledWith 测试匿名函数的匹配?
function toBeTested( id, values) {
return xyz(id, values, () => {
return {
type: 'foo',
payload: {
text: values.text
}
}
})
}
Run Code Online (Sandbox Code Playgroud)
在我的测试中
describe('test for toBeTested', () => {
it('should call xyz with params', () => {
const id = 123;
const values = {
text: 'Hello world',
};
xyz = jest.fn();
toBeTested(id, values);
expect(xyz).toHaveBeenCalledWith(id, values, () => {
return {
type: 'foo',
payload: {
text: values.text,
}
}
});
})
})
Run Code Online (Sandbox Code Playgroud)
测试错误报告
expect(jest.fn()).toHaveBeenCalledWith(expected)
Expected mock …
Run Code Online (Sandbox Code Playgroud)