Description
- Sinon version : 1.17.7
- Environment : node v6.9.2
- Example URL : None
- Other libraries you are using: mocha
What did you expect to happen?
I would expect that spy.callCount is number of times the target function was called.
And, spy.withArgs(arg1).callCount is number of times the target function was called with specific arguments.
But, the callCount property is often incorrect.
What actually happens
First of all, f(1)
and f(1, 1)
and f(1, 2)
is calling.
As this time, spy.withArgs(1).callCount
is 3
,
But spy.withArgs(1, 1).callCount
is often 0
.
This may be affected by calling order spy.withArgs()
.
Following test code has 2 case pattern, and change the order in which withArgs(1)
and withArgs(1, 1)
are called.
How to reproduce
const sinon = require('sinon'); // sinon@v1.17.7
const assert = require('referee').assert;
describe("#my-issue", function () {
it("case1", function () {
var obj = {};
obj.f = function () {};
var spy = sinon.spy(obj, "f");
// withArgs(1) => withArgs(1, 1)
assert.equals(spy.callCount, 0);
assert.equals(spy.withArgs(1).callCount, 0);
assert.equals(spy.withArgs(1, 1).callCount, 0);
obj.f();
obj.f(1);
obj.f(1, 1);
obj.f(1, 2);
assert.equals(spy.callCount, 4);
assert.equals(spy.withArgs(1).callCount, 3); // passed
assert.equals(spy.withArgs(1, 1).callCount, 1); // assertion-error
assert.equals(spy.withArgs(1, 2).callCount, 1); // passed
});
it("case2", function () {
var obj = {};
obj.f = function () {};
var spy = sinon.spy(obj, "f");
// change order, withArgs(1, 1) => withArgs(1)
assert.equals(spy.callCount, 0);
assert.equals(spy.withArgs(1, 1).callCount, 0);
assert.equals(spy.withArgs(1).callCount, 0);
obj.f();
obj.f(1);
obj.f(1, 1);
obj.f(1, 2);
assert.equals(spy.callCount, 4);
assert.equals(spy.withArgs(1).callCount, 3); // assertion error
assert.equals(spy.withArgs(1, 1).callCount, 1); // passed
assert.equals(spy.withArgs(1, 2).callCount, 1); // passed
});
});
Result of running above code.
% mocha test.js#my-issue
1) case1
2) case2
0 passing (21ms)
2 failing
-
#my-issue case1:
AssertionError: [assert.equals] 0 expected to be equal to 1
at referee.fail (node_modules/referee/lib/referee.js:193:25)
at Object.ctx.fail (node_modules/referee/lib/referee.js:90:21)
at assertion (node_modules/referee/lib/referee.js:98:21)
at Function.referee.(anonymous function).(anonymous function) [as equals] (node_modules/referee/lib/referee.js:118:23)
at Context. (test.js:23:12) -
#my-issue case2:
AssertionError: [assert.equals] 2 expected to be equal to 3
at referee.fail (node_modules/referee/lib/referee.js:193:25)
at Object.ctx.fail (node_modules/referee/lib/referee.js:90:21)
at assertion (node_modules/referee/lib/referee.js:98:21)
at Function.referee.(anonymous function).(anonymous function) [as equals] (node_modules/referee/lib/referee.js:118:23)
at Context. (test.js:43:12)
Activity
kbtknr commentedon Feb 23, 2017
If write the following hack before the assertion, this test will pass.
With the matchingFake function in spy.js, I think only the last fake (pop()) in fakes matched may be incremented.
kbtknr commentedon Feb 28, 2017
I think that stub is also affected by this issues.
Because stub depend on spy api, and this is similarly affected.
The following code is able to reproduce this bug.
Add test for issue sinonjs#1274
Fix sinonjs#1274: spy.withArgs(args...).callCount is incorrect
kbtknr commentedon Mar 1, 2017
This issues also happen in
v2.0.0-pre.6
.Could I send a PR to resolve this issues?
fatso83 commentedon Mar 1, 2017
Yes, please!
Add test for issue sinonjs#1274
Fix sinonjs#1274: spy.withArgs(args...).callCount is incorrect
Add test for sinonjs#1274 case3
kbtknr commentedon Mar 2, 2017
I investigate the part of
matchingFake
, in particularpop()
.It seems the feature of using withArgs function with passing sinon-matcher don't work.
For example:
I think case4 should prioritize non-matcher over matcher, and return its fake.
Current behavior is returning last matched fake (pop()).
So, the returning fake is undefined because of calling order
withArgs()
and making order fakes.In case5, stub do likewise.
But, this behavior is better to decide this strictly.
Seems like, case4 and case5 is different from root of issue, so should I open the new issue?
Move sinonjs#1274 test code to respective test files
13 remaining items