Skip to content

Commit

Permalink
toThrow()'s (and some other matchers') .not message is misleading.
Browse files Browse the repository at this point in the history
  • Loading branch information
xian committed Aug 26, 2010
1 parent 3ec736f commit b33b2d2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 3 additions & 2 deletions spec/suites/MatchersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
});
Expand Down
4 changes: 3 additions & 1 deletion src/Matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
}
Expand Down

0 comments on commit b33b2d2

Please sign in to comment.