Skip to content

Commit

Permalink
add calledOnceWithExactly assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
krzkaczor authored and mroderick committed Feb 10, 2018
1 parent b5968ab commit 3e7edca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/sinon/spy.js
Expand Up @@ -386,7 +386,7 @@ var spyApi = {
}
};

function delegateToCalls(method, matchAny, actual, notCalled) {
function delegateToCalls(method, matchAny, actual, notCalled, totalCallCount) {
spyApi[method] = function () {
if (!this.called) {
if (notCalled) {
Expand All @@ -395,6 +395,12 @@ function delegateToCalls(method, matchAny, actual, notCalled) {
return false;
}

if (totalCallCount !== undefined) {
if (this.callCount !== totalCallCount) {
return false;
}
}

var currentCall;
var matches = 0;

Expand Down Expand Up @@ -423,6 +429,7 @@ delegateToCalls("calledWithMatch", true);
delegateToCalls("alwaysCalledWith", false, "calledWith");
delegateToCalls("alwaysCalledWithMatch", false, "calledWithMatch");
delegateToCalls("calledWithExactly", true);
delegateToCalls("calledOnceWithExactly", true, "calledWithExactly", undefined, 1);
delegateToCalls("alwaysCalledWithExactly", false, "calledWithExactly");
delegateToCalls("neverCalledWith", false, "notCalledWith", function () {
return true;
Expand Down
32 changes: 32 additions & 0 deletions test/spy-test.js
Expand Up @@ -1181,6 +1181,38 @@ describe("spy", function () {
});
});

describe(".calledOnceWithExactly", function () {
beforeEach(function () {
this.spy = createSpy.create();
});

it("returns true for exact match", function () {
this.spy(1, 2, 3);

assert.isTrue(this.spy.calledOnceWithExactly(1, 2, 3));
});

it("returns false for exact parameters but called more then once", function () {
this.spy(1, 2, 3);
this.spy(1, 2, 3);

assert.isFalse(this.spy.calledOnceWithExactly(1, 2, 3));
});

it("return false for one mismatched call", function () {
this.spy(1, 2);

assert.isFalse(this.spy.calledOnceWithExactly(1, 2, 3));
});

it("return false for one mismatched call with some other ", function () {
this.spy(1, 2, 3);
this.spy(1, 2);

assert.isFalse(this.spy.calledOnceWithExactly(1, 2, 3));
});
});

describe(".alwaysCalledWithExactly", function () {
beforeEach(function () {
this.spy = createSpy.create();
Expand Down

0 comments on commit 3e7edca

Please sign in to comment.