Add custom messages for inverted spy matchers.

This commit is contained in:
Christian Williams 2010-08-13 00:32:32 -07:00
parent 9af81b627b
commit b3715075e3
2 changed files with 38 additions and 6 deletions

View File

@ -687,14 +687,30 @@ describe("jasmine.Matchers", function() {
}); });
it('should allow matches across multiple calls', function() { it('should allow matches across multiple calls', function() {
var expected = match(TestClass.spyFunction);
TestClass.spyFunction('a', 'b', 'c'); TestClass.spyFunction('a', 'b', 'c');
TestClass.spyFunction('d', 'e', 'f'); TestClass.spyFunction('d', 'e', 'f');
var expected = match(TestClass.spyFunction);
expect(expected.toHaveBeenCalledWith('a', 'b', 'c')).toPass(); expect(expected.toHaveBeenCalledWith('a', 'b', 'c')).toPass();
expect(expected.toHaveBeenCalledWith('d', 'e', 'f')).toPass(); expect(expected.toHaveBeenCalledWith('d', 'e', 'f')).toPass();
expect(expected.toHaveBeenCalledWith('x', 'y', 'z')).toFail(); expect(expected.toHaveBeenCalledWith('x', 'y', 'z')).toFail();
}); });
it("should return a decent message", function() {
TestClass.spyFunction('a', 'b', 'c');
TestClass.spyFunction('d', 'e', 'f');
var expected = match(TestClass.spyFunction);
expect(expected.toHaveBeenCalledWith('a', 'b')).toFail();
expect(lastResult().message).toEqual("Expected spy to have been called with [ 'a', 'b' ] but was called with [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ] ]");
});
it("should return a decent message when inverted", function() {
TestClass.spyFunction('a', 'b', 'c');
TestClass.spyFunction('d', 'e', 'f');
var expected = match(TestClass.spyFunction);
expect(expected.not.toHaveBeenCalledWith('a', 'b', 'c')).toFail();
expect(lastResult().message).toEqual("Expected spy not to have been called with [ 'a', 'b', 'c' ] but was called with [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ] ]");
});
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('toHaveBeenCalledWith')); it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('toHaveBeenCalledWith'));
describe("to build an ExpectationResult", function () { describe("to build an ExpectationResult", function () {

View File

@ -179,7 +179,10 @@ jasmine.Matchers.prototype.toHaveBeenCalled = function() {
} }
this.message = function() { this.message = function() {
return "Expected spy " + this.actual.identity + " to have been called."; return [
"Expected spy " + this.actual.identity + " to have been called.",
"Expected spy " + this.actual.identity + " not to have been called."
];
}; };
return this.actual.wasCalled; return this.actual.wasCalled;
@ -203,7 +206,10 @@ jasmine.Matchers.prototype.wasNotCalled = function() {
} }
this.message = function() { this.message = function() {
return "Expected spy " + this.actual.identity + " to not have been called."; return [
"Expected spy " + this.actual.identity + " to not have been called.",
"Expected spy " + this.actual.identity + " to have been called."
];
}; };
return !this.actual.wasCalled; return !this.actual.wasCalled;
@ -222,9 +228,16 @@ jasmine.Matchers.prototype.toHaveBeenCalledWith = function() {
} }
this.message = function() { this.message = function() {
if (this.actual.callCount == 0) { if (this.actual.callCount == 0) {
return "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was never called."; // todo: what should the failure message for .not.toHaveBeenCalledWith() be? is this right? test better. [xw]
return [
"Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.",
"Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was."
];
} else { } else {
return "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall); return [
"Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall),
"Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall)
];
} }
}; };
@ -242,7 +255,10 @@ jasmine.Matchers.prototype.wasNotCalledWith = function() {
} }
this.message = function() { this.message = function() {
return "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was"; return [
"Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was",
"Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was"
]
}; };
return !this.env.contains_(this.actual.argsForCall, expectedArgs); return !this.env.contains_(this.actual.argsForCall, expectedArgs);