d51*_*512 3 unit-testing sinon
我知道使用sinon.js可以测试间谍被多次调用:
sinon.assert.calledTwice(mySpy.someMethod);
Run Code Online (Sandbox Code Playgroud)
您可以测试是否使用某些参数调用了间谍:
sinon.assert.calledWith(mySpy.someMethod, 1, 2);
Run Code Online (Sandbox Code Playgroud)
但是,如何将它们组合起来以测试某个方法被调用了特定次数的特定参数呢?理论上是这样的:
sinon.assert.calledTwiceWith(mySpy.someMethod, 1, 2);
Run Code Online (Sandbox Code Playgroud)
A spy提供对使用getCall()和getCalls()进行的调用的访问。可以使用以下方法测试每个Spy调用calledWithExactly():
import * as sinon from 'sinon';
test('spy', () => {
const spy = sinon.spy();
spy(1, 2);
spy(3, 4);
expect(spy.callCount).toBe(2);
expect(spy.getCall(0).calledWithExactly(1, 2)).toBe(true);
expect(spy.getCall(1).calledWithExactly(3, 4)).toBe(true);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2110 次 |
| 最近记录: |