diff --git a/spec/suites/MatchersSpec.js b/spec/suites/MatchersSpec.js index 536bc48..7406f7c 100644 --- a/spec/suites/MatchersSpec.js +++ b/spec/suites/MatchersSpec.js @@ -687,14 +687,30 @@ describe("jasmine.Matchers", function() { }); it('should allow matches across multiple calls', function() { - var expected = match(TestClass.spyFunction); TestClass.spyFunction('a', 'b', 'c'); TestClass.spyFunction('d', 'e', 'f'); + var expected = match(TestClass.spyFunction); expect(expected.toHaveBeenCalledWith('a', 'b', 'c')).toPass(); expect(expected.toHaveBeenCalledWith('d', 'e', 'f')).toPass(); 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')); describe("to build an ExpectationResult", function () { diff --git a/src/Matchers.js b/src/Matchers.js index d85dd7c..3021e14 100644 --- a/src/Matchers.js +++ b/src/Matchers.js @@ -179,7 +179,10 @@ jasmine.Matchers.prototype.toHaveBeenCalled = 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; @@ -203,7 +206,10 @@ jasmine.Matchers.prototype.wasNotCalled = 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; @@ -222,9 +228,16 @@ jasmine.Matchers.prototype.toHaveBeenCalledWith = function() { } this.message = function() { 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 { - 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() { - 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);