-
-
Notifications
You must be signed in to change notification settings - Fork 769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
callCount property is incorrect #1274
Comments
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. |
I think that stub is also affected by this issues. it("case3", function() {
var stub = sinon.stub();
stub.returns(0);
stub.withArgs(1).returns(1);
stub.withArgs(1, 1).returns(2);
assert.equals(stub(), 0);
assert.equals(stub(1), 1);
assert.equals(stub(1, 1), 2); // assertion error, stub(1, 1) returns 1 actually.
assert.equals(stub(2), 0);
}); |
1. Update matchingFake function in spy.js Rename to matchingFakes, and return not last one fake (pop()) but all fakes matched. 2. Move matchingFakes function into spyApi Because of calling by stub.js 3. Modify spyApi.invoke function Apply processing at function invoked to all matching fakes. 4. Modify part of calling matchingFake() in spyApi.withArgs Fit logic, affected by 1. 5. Update proto.create.functionStub in stub.js Try to get the function stub using spyApi.matchingFakes() and pop(), affected by 1.
This issues also happen in Could I send a PR to resolve this issues? |
Yes, please! |
1. Update matchingFake function in spy.js Rename to matchingFakes, and return not last one fake (pop()) but all fakes matched. 2. Move matchingFakes function into spyApi Because of calling by stub.js 3. Modify spyApi.invoke function Apply processing at function invoked to all matching fakes. 4. Modify part of calling matchingFake() in spyApi.withArgs Fit logic, affected by 1. 5. Update proto.create.functionStub in stub.js Try to get the function stub using spyApi.matchingFakes() and pop(), affected by 1.
I investigate the part of For example:
I think case4 should prioritize non-matcher over matcher, and return its fake. 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? |
Fix #1274 callCount property is incorrect
1. Update matchingFake function in spy.js Rename to matchingFakes, and return not last one fake (pop()) but all fakes matched. 2. Move matchingFakes function into spyApi Because of calling by stub.js 3. Modify spyApi.invoke function Apply processing at function invoked to all matching fakes. 4. Modify part of calling matchingFake() in spyApi.withArgs Fit logic, affected by 1. 5. Update proto.create.functionStub in stub.js Try to get the function stub using spyApi.matchingFakes() and pop(), affected by 1.
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)
andf(1, 1)
andf(1, 2)
is calling.As this time,
spy.withArgs(1).callCount
is3
,But
spy.withArgs(1, 1).callCount
is often0
.This may be affected by calling order
spy.withArgs()
.Following test code has 2 case pattern, and change the order in which
withArgs(1)
andwithArgs(1, 1)
are called.How to reproduce
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)
The text was updated successfully, but these errors were encountered: