所以...我只是想检查我的 sut 是否以这种方式使用依赖项,即它至少被N次调用了正确的参数。我没有找到一种方法来做到这一点,而又没有肮脏的技巧来查找.mock.calls
条目并找到合适的条目。理想情况下,我希望有这样的代码:
it('...', () => {
const dependency = jest.fn();
const sut = createSut(dependency);
sut();
// that is of course wrong because of syntax but it shows what I want:
expect(dependency).toBeCalledWith({ some: 'arguments' }).times(5);
// this doesn't work - if at least one call was made it will always pass
for (let i = 0; i < 5: ++i)
expect(dependency).toBeCalledWith({ some: 'arguments' });
// this doesn't work either, 'cause at least one proper call will make tests pass
expect(dependency.mock.calls.length).toBeGreaterThanOrEqual(5);
expect(dependency).toBeCalledWith({ some: 'arguments' });
});
是否可以选择不使用jest
之类的其他库而在chai
中获得我想要的东西?
答案 0 :(得分:0)
您应该可以使用jests's matchers编写测试。
// Called just 5 times
expect(dependency).toHaveBeenCalledTimes(5);
// Called with same argument
[1,2,3,4,5].forEach(time => {
expect(dependency).toHaveBeenNthCalledWith(time, {
some: 'arguments',
});
});
// Called with different arguments
const expectedArguments = [
{some: 'arguments'},
null,
14,
{some:'arguments'},
{some:'arguments'},
];
expectedArguments.forEach((argument, index) => {
const callNumber = index + 1;
expect(dependency).toHaveBeenNthCalledWith(callNumber, argument);
});