Skip to content
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

add diff support for 'calledWith*' matchers #141

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions lib/sinon-chai.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,42 @@
});
}

function isDiffSupportedMethod(sinonMethodName) {
return (sinonMethodName.indexOf("calledWith") === 0) || (sinonMethodName.indexOf("calledOnceWith") === 0);
}

function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) {
return function () {
assertCanWorkWith(this);

var alwaysSinonMethod = "always" + sinonName[0].toUpperCase() + sinonName.substring(1);
var shouldBeAlways = utils.flag(this, "always") && typeof this._obj[alwaysSinonMethod] === "function";
var sinonMethodName = shouldBeAlways ? alwaysSinonMethod : sinonName;

var messages = getMessages(this._obj, action, nonNegatedSuffix, shouldBeAlways, slice.call(arguments));
var passed = this._obj[sinonMethodName].apply(this._obj, arguments);
var actual;
var expected;
var enableDiff;
if (!passed) {
if (isDiffSupportedMethod(sinonMethodName)) {
enableDiff = true;
var lastCall = this._obj.lastCall || this._obj;
actual = lastCall.args && lastCall.args;
if (this._obj.callCount === 0) {
actual = undefined;
}

expected = slice.call(arguments);
}
}

this.assert(
this._obj[sinonMethodName].apply(this._obj, arguments),
passed,
messages.affirmative,
messages.negative
messages.negative,
expected,
actual,
enableDiff
);
};
}
Expand Down
133 changes: 133 additions & 0 deletions test/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,139 @@ describe("Messages", function () {
});
});

describe("diff support", function () {
it("should add actual/expected and set enableDiff to true", function () {
var spy = sinon.spy();
spy("foo1");

var err;

try {
expect(spy).to.have.been.calledWith("bar");
} catch (e) {
err = e;
}
expect(err).ok;
expect(err).to.ownProperty("showDiff", true);
expect(err).to.ownProperty("actual").deep.eq(["foo1"]);
expect(err).to.ownProperty("expected").deep.eq(["bar"]);
});

it("should add diff when calledOnceWith matcher", function () {
var spy = sinon.spy();
spy("foo1");

var err;

try {
expect(spy).to.have.been.calledOnceWith("bar");
} catch (e) {
err = e;
}
expect(err).ok;
expect(err).to.ownProperty("showDiff", true);
expect(err).to.ownProperty("actual").deep.eq(["foo1"]);
expect(err).to.ownProperty("expected").deep.eq(["bar"]);
});

it("should add diff when calledOnceWithExactly matcher", function () {
var spy = sinon.spy();
spy("foo1");

var err;

try {
expect(spy).to.have.been.calledOnceWithExactly("bar");
} catch (e) {
err = e;
}
expect(err).ok;
expect(err).to.ownProperty("showDiff", true);
expect(err).to.ownProperty("actual").deep.eq(["foo1"]);
expect(err).to.ownProperty("expected").deep.eq(["bar"]);
});

it("should use lastCall args if exists", function () {
var spy = sinon.spy();
spy("foo1");
spy("foo2");

var err;

try {
expect(spy).to.have.been.calledWith("bar");
} catch (e) {
err = e;
}
expect(err).ok;
expect(err).to.ownProperty("showDiff", true);
expect(err).to.ownProperty("actual").deep.eq(["foo2"]);
expect(err).to.ownProperty("expected").deep.eq(["bar"]);
});

it("should use args if no lastCall", function () {
var spy = sinon.spy();
spy("foo1");
spy("foo2");

var err;

try {
expect(spy.firstCall).to.have.been.calledWith("bar");
} catch (e) {
err = e;
}
expect(err).ok;
expect(err).to.ownProperty("showDiff", true);
expect(err).to.ownProperty("actual").deep.eq(["foo1"]);
expect(err).to.ownProperty("expected").deep.eq(["bar"]);
});

it("should use undefined if no call", function () {
var spy = sinon.spy();

var err;

try {
expect(spy).to.have.been.calledWith("bar");
} catch (e) {
err = e;
}
expect(err).ok;
expect(err).to.ownProperty("showDiff", true);
expect(err).to.ownProperty("actual").deep.eq(undefined);
expect(err).to.ownProperty("expected").deep.eq(["bar"]);
});

it("should not add actual/expected and set enableDiff to true if passes", function () {
var spy = sinon.spy();
spy("foo", "bar", "baz");

var err;

try {
spy.should.calledWithExactly("foo", "bar", "baz");
} catch (e) {
err = e;
}
expect(err).to.be.undefined;
});

it("should not add actual/expected and set enableDiff if not 'calledWith' matcher", function () {
var spy = sinon.spy();
spy("foo", "bar", "baz");

var err;

try {
expect(spy).to.have.been.calledTwice();
} catch (e) {
err = e;
}
expect(err).ownProperty("showDiff").eq(false);
});
});

describe("when used on a non-spy/non-call", function () {
function notSpy() {
// Contents don't matter
Expand Down