From b33b2d2cf13c682d4e364df03122ee4f670d3976 Mon Sep 17 00:00:00 2001 From: Christian Williams Date: Wed, 25 Aug 2010 18:16:37 -0700 Subject: [PATCH] toThrow()'s (and some other matchers') .not message is misleading. --- spec/suites/MatchersSpec.js | 5 +++-- src/Matchers.js | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/spec/suites/MatchersSpec.js b/spec/suites/MatchersSpec.js index 7406f7c..dcfe889 100644 --- a/spec/suites/MatchersSpec.js +++ b/spec/suites/MatchersSpec.js @@ -500,6 +500,7 @@ describe("jasmine.Matchers", function() { describe("and matcher is inverted with .not", function() { it("should match any exception", function() { expect(match(throwingFn).not.toThrow()).toFail(); + expect(lastResult().message).toMatch(/Expected function not to throw an exception/); }); it("should match exceptions specified by message", function() { @@ -537,7 +538,7 @@ describe("jasmine.Matchers", function() { }); - describe("when code block doesn not throw an exception", function() { + describe("when code block does not throw an exception", function() { it("should fail (or pass when inverted with .not)", function() { expect(match(function() { }).toThrow()).toFail(); @@ -787,7 +788,7 @@ describe("jasmine.Matchers", function() { }); describe("all matchers", function() { - it("should return null, for futureproofing, since we might eventually allow matcher chaining", function() { + it("should return null, for future-proofing, since we might eventually allow matcher chaining", function() { expect(match(true).toBe(true)).toBeUndefined(); }); }); diff --git a/src/Matchers.js b/src/Matchers.js index 3021e14..46cffd6 100644 --- a/src/Matchers.js +++ b/src/Matchers.js @@ -311,9 +311,11 @@ jasmine.Matchers.prototype.toThrow = function(expected) { result = (expected === jasmine.undefined || this.env.equals_(exception.message || exception, expected.message || expected)); } + var not = this.isNot ? "not " : ""; + this.message = function() { if (exception && (expected === jasmine.undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) { - return ["Expected function to throw", expected ? expected.message || expected : " an exception", ", but it threw", exception.message || exception].join(' '); + return ["Expected function " + not + "to throw", expected ? expected.message || expected : " an exception", ", but it threw", exception.message || exception].join(' '); } else { return "Expected function to throw an exception."; }