Jestjs 笑话:测试内部函数被调用
鉴于以下情况,在执行Jestjs 笑话:测试内部函数被调用,jestjs,Jestjs,鉴于以下情况,在执行bar函数时,如何确保使用正确的消息调用内部foo函数?谢谢 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 execute
bar
函数时,如何确保使用正确的消息调用内部foo
函数?谢谢
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');
});
您需要像这样模拟
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')
})
谢谢@kaxi1993!我现在意识到这个答案并不能直接回答我的问题,因为我太抽象了。如果你好奇的话,我发布了一个更直接的问题。