Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinon Stub/Spy Using WithArgs Not Behaving As Expected

Tags:

sinon

When I specify withArgs for a sinon spy or stub, I expect the callCount to only count calls with those arguments. This doesn't seem to be happening, though.

If I run the following:

var mySpy = sinon.spy();
mySpy.withArgs("foo");

mySpy("bar");

expect(mySpy.callCount).to.be(0);

I get "expected 1 to equal 0". Am I crazy, is this a bug, or is there another way to do this?

like image 872
Josh Schultz Avatar asked Jun 25 '13 16:06

Josh Schultz


People also ask

How do I reset my Sinon stub?

var stub = sinon. The original function can be restored by calling object. method. restore(); (or stub. restore(); ).

How does Sinon spy work?

The function sinon. spy returns a Spy object, which can be called like a function, but also contains properties with information on any calls made to it. In the example above, the firstCall property has information about the first call, such as firstCall. args which is the list of arguments passed.

Which of the following Sinon test function that records arguments return value the value of this and exception thrown if any for all its calls?

A test spy is a function that records arguments, return value, the value of this and exception thrown (if any) for all its calls.


1 Answers

You have to add withArgs to the assertion, too, like so:

var mySpy = sinon.spy();
mySpy.withArgs("foo");

mySpy("bar");

expect(mySpy.withArgs("foo").callCount).to.be(0);
like image 67
Josh Schultz Avatar answered Jan 01 '23 12:01

Josh Schultz