我需要测试一个在浏览器中打开新标签页的函数
openStatementsReport(contactIds) {
window.open(`a_url_${contactIds}`);
}
我希望模拟 window 的open
函数,以便验证正确的URL被传递给open
函数。
使用Jest,我不知道如何模拟window
。我尝试使用模拟函数设置window.open
,但是这种方法不起作用。以下是测试案例:
it('the correct URL is called', () => {
window.open = jest.fn();
statementService.openStatementsReport(111);
expect(window.open).toBeCalled();
});
但是它给我报错了
expect(jest.fn())[.not].toBeCalled()
jest.fn() value must be a mock function or spy.
Received:
function: [Function anonymous]
我该怎么处理这个测试用例?