鉴于以下情况,当执行foo
函数时,如何确保使用正确的消息调用内部bar
函数?谢谢。
const foo = (message) => console.log(message);
const bar = () => foo('this is a message');
test('test that the foo function is called with the correct message when the bar' +
' function is executed', () => {
bar();
expect(foo).toHaveBeenCalledWith('this is a message');
});
答案 0 :(得分:1)
您需要像这样模拟foo
函数:
let foo = message => console.log(message)
const bar = () => foo('this is a message')
test('test that the foo function is called with the correct message when the bar function is executed', () => {
foo = jest.fn()
bar()
expect(foo).toHaveBeenCalledWith('this is a message')
})