Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 22 additions & 2 deletions lib/sinon-chai.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,30 @@
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;
if (!passed) {
if (sinonMethodName.indexOf("calledWith") === 0) {
var lastCall = this._obj.lastCall || this._obj;
actual = lastCall.args && lastCall.args;
if (this._obj.callCount === 0) {
actual = undefined;
}

expected = slice.call(arguments);
}
}

var enableDiff = !passed;

this.assert(
this._obj[sinonMethodName].apply(this._obj, arguments),
passed,
messages.affirmative,
messages.negative
messages.negative,
expected,
actual,
enableDiff
);
};
}
Expand Down
99 changes: 99 additions & 0 deletions test/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,105 @@ 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 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