Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinon.js combining calledWith number of times

I know with sinon.js you can test that a spy was called a certain number of times:

sinon.assert.calledTwice(mySpy.someMethod);

And you can test that a spy was called with certain arguments:

sinon.assert.calledWith(mySpy.someMethod, 1, 2);

But how to you combine them to test that a method was called a specific number of times with specific arguments? Something, theoretically, like this:

sinon.assert.calledTwiceWith(mySpy.someMethod, 1, 2);
like image 782
d512 Avatar asked Aug 29 '18 04:08

d512


1 Answers

A spy provides access to the calls made to it using getCall() and getCalls(). Each Spy call can be tested using methods like 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);

});
like image 60
Brian Adams Avatar answered Sep 28 '22 06:09

Brian Adams