Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jestjs 笑话:测试内部函数被调用_Jestjs - Fatal编程技术网

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!我现在意识到这个答案并不能直接回答我的问题,因为我太抽象了。如果你好奇的话,我发布了一个更直接的问题。