@@ -1,6 +1,7 @@
-(function (sinonChai) {
- "use strict";
+"use strict";
+/* eslint-disable no-invalid-this */
+(function (sinonChai) {
// Module systems magic dance.
/* istanbul ignore else */
@@ -14,13 +15,15 @@
});
} else {
// Other environment (usually <script> tag): plug in to global chai instance directly.
+ /* global chai: false */
chai.use(sinonChai);
}
-}(function sinonChai(chai, utils) {
- "use strict";
-
+}(function (chai, utils) {
var slice = Array.prototype.slice;
+ var callsSuffix = "\n\n The following calls were made:\n%C";
+ var noCallsSuffix = ", but it was never called";
+
function isSpy(putativeSpy) {
return typeof putativeSpy === "function" &&
typeof putativeSpy.getCall === "function" &&
@@ -28,10 +31,20 @@
}
function timesInWords(count) {
- return count === 1 ? "once" :
- count === 2 ? "twice" :
- count === 3 ? "thrice" :
- (count || 0) + " times";
+ switch (count) {
+ case 1: {
+ return "once";
+ }
+ case 2: {
+ return "twice";
+ }
+ case 3: {
+ return "thrice";
+ }
+ default: {
+ return (count || 0) + " times";
+ }
+ }
}
function isCall(putativeCall) {
@@ -48,10 +61,20 @@
var verbPhrase = always ? "always have " : "have ";
passedSuffix = passedSuffix || "";
failedSuffix = failedSuffix || "";
+
if (isSpy(spy.proxy)) {
spy = spy.proxy;
}
+ if (spy.getCalls().length) {
+ // If there were calls, display them in case of a failure
+ failedSuffix += callsSuffix;
+ } else {
+ // Otherwise, replace the entire failed suffix with message that
+ // there were no calls
+ failedSuffix = noCallsSuffix;
+ }
+
function printfArray(array) {
return spy.printf.apply(spy, array);
}
@@ -97,9 +120,9 @@
var alwaysSinonMethod = "always" + sinonName[0].toUpperCase() + sinonName.substring(1);
var shouldBeAlways = utils.flag(this, "always") && typeof this._obj[alwaysSinonMethod] === "function";
- var sinonMethod = shouldBeAlways ? alwaysSinonMethod : sinonName;
+ var sinonMethodName = shouldBeAlways ? alwaysSinonMethod : sinonName;
- var expression = this._obj[sinonMethod].apply(this._obj, arguments);
+ var expression = this._obj[sinonMethodName].apply(this._obj, arguments);
var passed = utils.test(this, [expression]);
var messages = getMessages(passed,
this._obj, action, passedSuffix, failedSuffix, shouldBeAlways, slice.call(arguments));
@@ -125,20 +148,20 @@
utils.flag(this, "always", true);
});
- var calls = "\n\n The following calls were made:\n%C";
-
- sinonProperty("called", "been called", " at least once", " at least once, but it was never called");
- sinonPropertyAsBooleanMethod("callCount", "been called exactly %1", "", ", but it was called %c" + calls);
- sinonProperty("calledOnce", "been called exactly once", "", ", but it was called %c" + calls);
- sinonProperty("calledTwice", "been called exactly twice", "", ", but it was called %c" + calls);
- sinonProperty("calledThrice", "been called exactly thrice", "", ", but it was called %c" + calls);
+ sinonProperty("called", "been called at least once");
+ sinonPropertyAsBooleanMethod("callCount", "been called exactly %1", "", ", but it was called %c");
+ sinonProperty("calledOnce", "been called exactly once", "", ", but it was called %c");
+ sinonProperty("calledTwice", "been called exactly twice", "", ", but it was called %c");
+ sinonProperty("calledThrice", "been called exactly thrice", "", ", but it was called %c");
sinonMethodAsProperty("calledWithNew", "been called with new");
sinonMethod("calledBefore", "been called before %1");
sinonMethod("calledAfter", "been called after %1");
sinonMethod("calledOn", "been called with %1 as this", "", ", but it was called with %t instead");
- sinonMethod("calledWith", "been called with arguments %*", "", calls);
- sinonMethod("calledWithExactly", "been called with exact arguments %*", "", calls);
- sinonMethod("calledWithMatch", "been called with arguments matching %*", "", calls);
+ sinonMethod("calledWith", "been called with arguments %*");
+ sinonMethod("calledWithExactly", "been called with exact arguments %*");
+ sinonMethod("calledWithMatch", "been called with arguments matching %*");
+ sinonMethod("calledImmediatelyBefore", "been called immediately before %1");
+ sinonMethod("calledImmediatelyAfter", "been called immediately after %1");
sinonMethod("returned", "returned %1");
exceptionalSinonMethod("thrown", "threw", "thrown %1");
}));