Refactor toThrow() matcher specs.

This commit is contained in:
Christian Williams 2010-08-03 18:51:31 -07:00
parent 4d7b839473
commit 676af93bea
2 changed files with 78 additions and 32 deletions

2
pages

@ -1 +1 @@
Subproject commit 127fa05a6d35e59d1630ee741d6d145894b1fb98 Subproject commit 7820629e18863f2819a85062bc1ca5fe4f4cee6b

View File

@ -20,6 +20,15 @@ describe("jasmine.Matchers", function() {
return spec.addMatcherResult.mostRecentCall.args[0]; return spec.addMatcherResult.mostRecentCall.args[0];
} }
function catchException(fn) {
try {
fn.call();
} catch (e) {
return e;
}
throw new Error("expected function to throw an exception");
}
it("toEqual with primitives, objects, dates, etc.", function() { it("toEqual with primitives, objects, dates, etc.", function() {
expect(match(true).toEqual(true)).toEqual(true); expect(match(true).toEqual(true)).toEqual(true);
@ -453,42 +462,79 @@ describe("jasmine.Matchers", function() {
expect(result.expected).toEqual(expected); expect(result.expected).toEqual(expected);
}); });
it("toThrow", function() { describe("toThrow", function() {
var expected = match(function() { describe("when code block throws an exception", function() {
throw new Error("Fake Error"); var throwingFn;
beforeEach(function() {
throwingFn = function() {
throw new Error("Fake Error");
};
});
it("should match any exception", function() {
expect(match(throwingFn).toThrow()).toEqual(true);
});
it("should match exceptions specified by message", function() {
expect(match(throwingFn).toThrow("Fake Error")).toEqual(true);
expect(match(throwingFn).toThrow("Other Error")).toEqual(false);
expect(lastResult().message).toMatch("Other Error");
});
it("should match exceptions specified by Error", function() {
expect(match(throwingFn).toThrow(new Error("Fake Error"))).toEqual(true);
expect(match(throwingFn).toThrow(new Error("Other Error"))).toEqual(false);
expect(lastResult().message).toMatch("Other Error");
});
describe("and matcher is inverted with .not", function() {
it("should match any exception", function() {
expect(match(throwingFn).not.toThrow()).toEqual(false);
});
it("should match exceptions specified by message", function() {
expect(match(throwingFn).not.toThrow("Fake Error")).toEqual(false);
// expect(lastResult().message).toMatch(/Expected function not to throw Fake Error./);
expect(match(throwingFn).not.toThrow("Other Error")).toEqual(true);
});
it("should match exceptions specified by Error", function() {
expect(match(throwingFn).not.toThrow(new Error("Fake Error"))).toEqual(false);
// expect(lastResult().message).toMatch("Other Error");
expect(match(throwingFn).not.toThrow(new Error("Other Error"))).toEqual(true);
});
});
}); });
expect(expected.toThrow()).toEqual(true); describe("when actual is not a function", function() {
expect(expected.not.toThrow()).toEqual(false); it("should fail with an exception", function() {
var exception = catchException(function() {
match('not-a-function').toThrow();
});
expect(exception).toBeDefined();
expect(exception.message).toEqual('Actual is not a function');
});
expect(expected.toThrow("Fake Error")).toEqual(true); describe("and matcher is inverted with .not", function() {
expect(expected.toThrow(new Error("Fake Error"))).toEqual(true); it("should fail with an exception", function() {
var exception = catchException(function() {
expect(expected.toThrow("Other Error")).toEqual(false); match('not-a-function').not.toThrow();
var result = lastResult(); });
expect(result.message).toMatch("Other Error"); expect(exception).toBeDefined();
expect(exception.message).toEqual('Actual is not a function');
expect(expected.toThrow(new Error("Other Error"))).toEqual(false); });
result = lastResult(); });
expect(result.message).toMatch("Other Error"); });
var exception;
try {
(function () {
new jasmine.Matchers(env, 'not-a-function', spec).toThrow();
})();
} catch (e) {
exception = e;
}
expect(exception).toBeDefined();
expect(exception.message).toEqual('Actual is not a function');
expect(match(function() { describe("when code block doesn not throw an exception", function() {
}).toThrow()).toEqual(false); it("should fail (or pass when inverted with .not)", function() {
result = lastResult(); expect(match(function() {
expect(result.message).toEqual('Expected function to throw an exception.'); }).toThrow()).toEqual(false);
expect(lastResult().message).toEqual('Expected function to throw an exception.');
});
});
}); });
describe(".not.matcher", function() { describe(".not.matcher", function() {