Шутка: тестирование внутренней функции называется - PullRequest
1 голос
/ 23 апреля 2019

Учитывая приведенное ниже, как мне убедиться, что внутренняя функция 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');
});

1 Ответ

1 голос
/ 23 апреля 2019

Вам нужно смоделировать 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')
})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...