Skip to content

Commit

Permalink
Move #1274 test code to respective test files
Browse files Browse the repository at this point in the history
  • Loading branch information
takasmiley committed Jun 28, 2017
1 parent 833ff2c commit cf3a34e
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
Expand Up @@ -270,61 +270,4 @@ describe("issues", function () {
});
});
}

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
Expand Up @@ -326,6 +326,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
Expand Up @@ -102,6 +102,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 cf3a34e

Please sign in to comment.