Skip to content

Commit

Permalink
Merge pull request #1478 from takasmiley/issues/#1476
Browse files Browse the repository at this point in the history
Fix #1476: stub#firstCall seems broken on sinon v2.3.6
  • Loading branch information
fatso83 committed Jul 3, 2017
2 parents 8ad2ed7 + 40efa6f commit 079925b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
3 changes: 3 additions & 0 deletions lib/sinon/spy.js
Expand Up @@ -179,6 +179,9 @@ var spyApi = {

// Make call properties available from within the spied function:
createCallProperties.call(this);
matchings.forEach(function (matching) {
createCallProperties.call(matching);
});

try {
this.invoking = true;
Expand Down
74 changes: 63 additions & 11 deletions test/spy-test.js
Expand Up @@ -326,7 +326,59 @@ describe("spy", function () {
});
});

it("counts with combination of withArgs arguments and order of calling withArgs", function () {
it("should work with combination of withArgs arguments and order of calling withArgs", function () {
function assertSpy(spy) {
// assert callCount
assert.equals(spy.callCount, 4);
assert.equals(spy.withArgs(1).callCount, 3);
assert.equals(spy.withArgs(1, 1).callCount, 1);
assert.equals(spy.withArgs(1, 2).callCount, 1);

// assert call
refute.defined(spy.getCall(0).args[0]);
assert.equals(spy.getCall(1).args[0], 1);
refute.defined(spy.getCall(1).args[1]);
assert.equals(spy.getCall(2).args[0], 1);
assert.equals(spy.getCall(2).args[1], 1);
refute.defined(spy.getCall(2).args[2]);
assert.equals(spy.getCall(3).args[0], 1);
assert.equals(spy.getCall(3).args[1], 2);
refute.defined(spy.getCall(3).args[2]);
["args", "callCount", "callId"].forEach(function (propName) {
assert.equals(spy.withArgs(1).getCall(0)[propName],
spy.getCall(1)[propName]);
assert.equals(spy.withArgs(1).getCall(1)[propName],
spy.getCall(2)[propName]);
assert.equals(spy.withArgs(1).getCall(2)[propName],
spy.getCall(3)[propName]);
assert.isNull(spy.withArgs(1).getCall(3));
assert.equals(spy.withArgs(1, 1).getCall(0)[propName],
spy.getCall(2)[propName]);
assert.isNull(spy.withArgs(1, 1).getCall(1));
assert.equals(spy.withArgs(1, 2).getCall(0)[propName],
spy.getCall(3)[propName]);
assert.isNull(spy.withArgs(1, 2).getCall(1));
});

// assert firstCall, secondCall, thirdCall, and lastCall
assert.equals(spy.firstCall.callId, spy.getCall(0).callId);
assert.equals(spy.secondCall.callId, spy.getCall(1).callId);
assert.equals(spy.thirdCall.callId, spy.getCall(2).callId);
assert.equals(spy.lastCall.callId, spy.getCall(3).callId);
assert.equals(spy.withArgs(1).firstCall.callId, spy.withArgs(1).getCall(0).callId);
assert.equals(spy.withArgs(1).secondCall.callId, spy.withArgs(1).getCall(1).callId);
assert.equals(spy.withArgs(1).thirdCall.callId, spy.withArgs(1).getCall(2).callId);
assert.equals(spy.withArgs(1).lastCall.callId, spy.withArgs(1).getCall(2).callId);
assert.equals(spy.withArgs(1, 1).firstCall.callId, spy.withArgs(1, 1).getCall(0).callId);
assert.isNull(spy.withArgs(1, 1).secondCall);
assert.isNull(spy.withArgs(1, 1).thirdCall);
assert.equals(spy.withArgs(1, 1).lastCall.callId, spy.withArgs(1, 1).getCall(0).callId);
assert.equals(spy.withArgs(1, 2).firstCall.callId, spy.withArgs(1, 2).getCall(0).callId);
assert.isNull(spy.withArgs(1, 2).secondCall);
assert.isNull(spy.withArgs(1, 2).thirdCall);
assert.equals(spy.withArgs(1, 2).lastCall.callId, spy.withArgs(1, 2).getCall(0).callId);
}

var object = {
f1: function () {},
f2: function () {}
Expand All @@ -337,32 +389,32 @@ describe("spy", function () {
assert.equals(spy1.callCount, 0);
assert.equals(spy1.withArgs(1).callCount, 0);
assert.equals(spy1.withArgs(1, 1).callCount, 0);
assert.isNull(spy1.getCall(0));
assert.isNull(spy1.getCall(1));
assert.isNull(spy1.getCall(2));
assert.isNull(spy1.getCall(3));

object.f1();
object.f1(1);
object.f1(1, 1);
object.f1(1, 2);

assert.equals(spy1.callCount, 4);
assert.equals(spy1.withArgs(1).callCount, 3);
assert.equals(spy1.withArgs(1, 1).callCount, 1);
assert.equals(spy1.withArgs(1, 2).callCount, 1);
assertSpy(spy1);

// f2: the order of withArgs(1, 1), withArgs(1)
var spy2 = createSpy(object, "f2");
assert.equals(spy2.callCount, 0);
assert.equals(spy2.withArgs(1, 1).callCount, 0);
assert.equals(spy2.withArgs(1).callCount, 0);
assert.isNull(spy2.getCall(0));
assert.isNull(spy2.getCall(1));
assert.isNull(spy2.getCall(2));
assert.isNull(spy2.getCall(3));

object.f2();
object.f2(1);
object.f2(1, 1);
object.f2(1, 2);

assert.equals(spy2.callCount, 4);
assert.equals(spy2.withArgs(1).callCount, 3);
assert.equals(spy2.withArgs(1, 1).callCount, 1);
assert.equals(spy2.withArgs(1, 2).callCount, 1);
assertSpy(spy2);
});

describe(".named", function () {
Expand Down
9 changes: 9 additions & 0 deletions test/stub-test.js
Expand Up @@ -115,6 +115,15 @@ describe("stub", function () {
assert.equals(stub(2), 0);
});

it("should work with combination of withArgs arguments", function () {
var stub = createStub();

stub.withArgs(1).returns(42);
stub(1);

refute.isNull(stub.withArgs(1).firstCall);
});

describe(".returns", function () {
it("returns specified value", function () {
var stub = createStub.create();
Expand Down

0 comments on commit 079925b

Please sign in to comment.