Restore custom failure messages for toHaveBeenCalledWith
As pointed out by @tjgrathwell
This commit is contained in:
@@ -2114,21 +2114,26 @@ getJasmineRequireObj().toHaveBeenCalledWith = function(j$) {
|
|||||||
compare: function() {
|
compare: function() {
|
||||||
var args = Array.prototype.slice.call(arguments, 0),
|
var args = Array.prototype.slice.call(arguments, 0),
|
||||||
actual = args[0],
|
actual = args[0],
|
||||||
expectedArgs = args.slice(1);
|
expectedArgs = args.slice(1),
|
||||||
|
result = { pass: false };
|
||||||
|
|
||||||
if (!j$.isSpy(actual)) {
|
if (!j$.isSpy(actual)) {
|
||||||
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
|
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
if (!actual.calls.any()) {
|
||||||
pass: util.contains(actual.calls.allArgs(), expectedArgs)
|
result.message = "Expected spy " + actual.and.identity() + " to have been called with " + j$.pp(expectedArgs) + " but it was never called.";
|
||||||
};
|
return result;
|
||||||
},
|
}
|
||||||
message: function(actual) {
|
|
||||||
return {
|
if (util.contains(actual.calls.allArgs(), expectedArgs)) {
|
||||||
affirmative: "Expected spy " + actual.and.identity() + " to have been called.",
|
result.pass = true;
|
||||||
negative: "Expected spy " + actual.and.identity() + " not to have been called."
|
result.message = "Expected spy " + actual.and.identity() + " not to have been called with " + j$.pp(expectedArgs) + " but it was.";
|
||||||
};
|
} else {
|
||||||
|
result.message = "Expected spy " + actual.and.identity() + " to have been called with " + j$.pp(expectedArgs) + " but actual calls were " + j$.pp(actual.calls.allArgs()).replace(/^\[ | \]$/g, '') + ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ describe("toHaveBeenCalledWith", function() {
|
|||||||
result = matcher.compare(calledSpy, 'a', 'b');
|
result = matcher.compare(calledSpy, 'a', 'b');
|
||||||
|
|
||||||
expect(result.pass).toBe(true);
|
expect(result.pass).toBe(true);
|
||||||
|
expect(result.message).toEqual("Expected spy called-spy not to have been called with [ 'a', 'b' ] but it was.");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("fails when the actual was not called", function() {
|
it("fails when the actual was not called", function() {
|
||||||
@@ -23,6 +24,7 @@ describe("toHaveBeenCalledWith", function() {
|
|||||||
|
|
||||||
result = matcher.compare(uncalledSpy);
|
result = matcher.compare(uncalledSpy);
|
||||||
expect(result.pass).toBe(false);
|
expect(result.pass).toBe(false);
|
||||||
|
expect(result.message).toEqual("Expected spy uncalled spy to have been called with [ ] but it was never called.");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("fails when the actual was called with different parameters", function() {
|
it("fails when the actual was called with different parameters", function() {
|
||||||
@@ -34,9 +36,11 @@ describe("toHaveBeenCalledWith", function() {
|
|||||||
result;
|
result;
|
||||||
|
|
||||||
calledSpy('a');
|
calledSpy('a');
|
||||||
|
calledSpy('c', 'd');
|
||||||
result = matcher.compare(calledSpy, 'a', 'b');
|
result = matcher.compare(calledSpy, 'a', 'b');
|
||||||
|
|
||||||
expect(result.pass).toBe(false);
|
expect(result.pass).toBe(false);
|
||||||
|
expect(result.message).toEqual("Expected spy called spy to have been called with [ 'a', 'b' ] but actual calls were [ 'a' ], [ 'c', 'd' ].");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("throws an exception when the actual is not a spy", function() {
|
it("throws an exception when the actual is not a spy", function() {
|
||||||
@@ -45,13 +49,4 @@ describe("toHaveBeenCalledWith", function() {
|
|||||||
|
|
||||||
expect(function() { matcher.compare(fn) }).toThrow(new Error("Expected a spy, but got Function."));
|
expect(function() { matcher.compare(fn) }).toThrow(new Error("Expected a spy, but got Function."));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("has a custom message on failure", function() {
|
|
||||||
var matcher = j$.matchers.toHaveBeenCalledWith(),
|
|
||||||
spy = j$.createSpy('sample-spy'),
|
|
||||||
messages = matcher.message(spy);
|
|
||||||
|
|
||||||
expect(messages.affirmative).toEqual("Expected spy sample-spy to have been called.")
|
|
||||||
expect(messages.negative).toEqual("Expected spy sample-spy not to have been called.")
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,21 +5,26 @@ getJasmineRequireObj().toHaveBeenCalledWith = function(j$) {
|
|||||||
compare: function() {
|
compare: function() {
|
||||||
var args = Array.prototype.slice.call(arguments, 0),
|
var args = Array.prototype.slice.call(arguments, 0),
|
||||||
actual = args[0],
|
actual = args[0],
|
||||||
expectedArgs = args.slice(1);
|
expectedArgs = args.slice(1),
|
||||||
|
result = { pass: false };
|
||||||
|
|
||||||
if (!j$.isSpy(actual)) {
|
if (!j$.isSpy(actual)) {
|
||||||
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
|
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
if (!actual.calls.any()) {
|
||||||
pass: util.contains(actual.calls.allArgs(), expectedArgs)
|
result.message = "Expected spy " + actual.and.identity() + " to have been called with " + j$.pp(expectedArgs) + " but it was never called.";
|
||||||
};
|
return result;
|
||||||
},
|
}
|
||||||
message: function(actual) {
|
|
||||||
return {
|
if (util.contains(actual.calls.allArgs(), expectedArgs)) {
|
||||||
affirmative: "Expected spy " + actual.and.identity() + " to have been called.",
|
result.pass = true;
|
||||||
negative: "Expected spy " + actual.and.identity() + " not to have been called."
|
result.message = "Expected spy " + actual.and.identity() + " not to have been called with " + j$.pp(expectedArgs) + " but it was.";
|
||||||
};
|
} else {
|
||||||
|
result.message = "Expected spy " + actual.and.identity() + " to have been called with " + j$.pp(expectedArgs) + " but actual calls were " + j$.pp(actual.calls.allArgs()).replace(/^\[ | \]$/g, '') + ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user