玩笑-如何使用一次期望来断言是否调用了多个函数

问题描述 投票:0回答:1

我已经使用Jest一段时间了,我知道我可以使用倍数expect来测试每个功能,例如:

expect(myFunction).toBeCalled();

因此,我想知道是否有一种方法可以使用单个e expect方法来测试多个功能。类似于:

const logoutModalProps = {
  dismissAllModals: mockFunction,
  logoutUserDeleteState: mockFunction,
  toggleDrawer: mockFunction,
  resetToScene: mockFunction,
  popScene: mockFunction,
};

describe('when using handlers for logoutModal', () => {
  it('should call handleLogout methods', () => {
    handleLogout(logoutModalProps);
    expect(...logoutModalProps).toBeCalled();
  });
});

注:在这种情况下,函数handleLogout调用所有这些方法。

有办法吗?如果是,请告诉我。如果没有,我应该使用多个期望还是有更好的方法?

感谢前进

javascript node.js jest
1个回答
0
投票

只需使用多个期望。如果这样做很麻烦,则可以循环执行。

it('should call handleLogout methods', () => {
  handleLogout(logoutModalProps);
  Object.values(logoutModalProps).forEach(mockFn => {
   expect(mockFn).toBeCalled();
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.