Skip to content

Commit

Permalink
Move sinonjs#1274 test code to respective test files
Browse files Browse the repository at this point in the history
  • Loading branch information
takasmiley committed Jun 23, 2017
1 parent e11b3ac commit 2087209
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 57 deletions.
57 changes: 0 additions & 57 deletions test/issues/issues-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,61 +200,4 @@ describe("issues", function () {
assert(firstFake !== secondFake);
});
});

describe("#1274 - spy.withArgs(args...).callCount is incorrect", function () {
it("case1: should count accurately", function () {
var obj = {
f: function () {}
};

// case1: withArgs(1) => withArgs(1, 1)
var spy = sinon.spy(obj, "f");
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);
assert.equals(spy.withArgs(1, 1).callCount, 1);
assert.equals(spy.withArgs(1, 2).callCount, 1);
});
it("case2: should count accurately", function () {
var obj = {
f: function () {}
};

// case2: withArgs(1, 1) => withArgs(1)
var spy = sinon.spy(obj, "f");
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);
assert.equals(spy.withArgs(1, 1).callCount, 1);
assert.equals(spy.withArgs(1, 2).callCount, 1);
});
it("case3: should work on stub", function () {
var stub = sinon.stub();
stub.returns(0);
stub.withArgs(1, 1).returns(2);
stub.withArgs(1).returns(1);

assert.equals(stub(), 0);
assert.equals(stub(1), 1);
assert.equals(stub(1, 1), 2);
assert.equals(stub(1, 1, 1), 2);
assert.equals(stub(2), 0);
});
});
});
39 changes: 39 additions & 0 deletions test/spy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,45 @@ describe("spy", function () {
}
});

it("counts with combination of withArgs arguments and order of calling withArgs", function () {
var object = {
f1: function () {},
f2: function () {}
};

// f1: the order of withArgs(1), withArgs(1, 1)
var spy1 = createSpy(object, "f1");
assert.equals(spy1.callCount, 0);
assert.equals(spy1.withArgs(1).callCount, 0);
assert.equals(spy1.withArgs(1, 1).callCount, 0);

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);

// 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);

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);
});

describe(".named", function () {
it("sets displayName", function () {
var spy = createSpy();
Expand Down
13 changes: 13 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ describe("stub", function () {
assert(callback.called);
});

it("should works with combination of withArgs arguments", function () {
var stub = createStub();
stub.returns(0);
stub.withArgs(1, 1).returns(2);
stub.withArgs(1).returns(1);

assert.equals(stub(), 0);
assert.equals(stub(1), 1);
assert.equals(stub(1, 1), 2);
assert.equals(stub(1, 1, 1), 2);
assert.equals(stub(2), 0);
});

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

0 comments on commit 2087209

Please sign in to comment.