Refactor toThrow() matcher specs.
This commit is contained in:
parent
4d7b839473
commit
676af93bea
2
pages
2
pages
|
@ -1 +1 @@
|
||||||
Subproject commit 127fa05a6d35e59d1630ee741d6d145894b1fb98
|
Subproject commit 7820629e18863f2819a85062bc1ca5fe4f4cee6b
|
|
@ -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() {
|
||||||
|
var throwingFn;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
throwingFn = function() {
|
||||||
throw new Error("Fake Error");
|
throw new Error("Fake Error");
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(expected.toThrow()).toEqual(true);
|
it("should match any exception", function() {
|
||||||
expect(expected.not.toThrow()).toEqual(false);
|
expect(match(throwingFn).toThrow()).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
expect(expected.toThrow("Fake Error")).toEqual(true);
|
it("should match exceptions specified by message", function() {
|
||||||
expect(expected.toThrow(new Error("Fake Error"))).toEqual(true);
|
expect(match(throwingFn).toThrow("Fake Error")).toEqual(true);
|
||||||
|
expect(match(throwingFn).toThrow("Other Error")).toEqual(false);
|
||||||
|
expect(lastResult().message).toMatch("Other Error");
|
||||||
|
});
|
||||||
|
|
||||||
expect(expected.toThrow("Other Error")).toEqual(false);
|
it("should match exceptions specified by Error", function() {
|
||||||
var result = lastResult();
|
expect(match(throwingFn).toThrow(new Error("Fake Error"))).toEqual(true);
|
||||||
expect(result.message).toMatch("Other Error");
|
expect(match(throwingFn).toThrow(new Error("Other Error"))).toEqual(false);
|
||||||
|
expect(lastResult().message).toMatch("Other Error");
|
||||||
|
});
|
||||||
|
|
||||||
expect(expected.toThrow(new Error("Other Error"))).toEqual(false);
|
describe("and matcher is inverted with .not", function() {
|
||||||
result = lastResult();
|
it("should match any exception", function() {
|
||||||
expect(result.message).toMatch("Other Error");
|
expect(match(throwingFn).not.toThrow()).toEqual(false);
|
||||||
|
});
|
||||||
|
|
||||||
var exception;
|
it("should match exceptions specified by message", function() {
|
||||||
try {
|
expect(match(throwingFn).not.toThrow("Fake Error")).toEqual(false);
|
||||||
(function () {
|
// expect(lastResult().message).toMatch(/Expected function not to throw Fake Error./);
|
||||||
new jasmine.Matchers(env, 'not-a-function', spec).toThrow();
|
expect(match(throwingFn).not.toThrow("Other Error")).toEqual(true);
|
||||||
})();
|
});
|
||||||
} catch (e) {
|
|
||||||
exception = e;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when actual is not a function", function() {
|
||||||
|
it("should fail with an exception", function() {
|
||||||
|
var exception = catchException(function() {
|
||||||
|
match('not-a-function').toThrow();
|
||||||
|
});
|
||||||
expect(exception).toBeDefined();
|
expect(exception).toBeDefined();
|
||||||
expect(exception.message).toEqual('Actual is not a function');
|
expect(exception.message).toEqual('Actual is not a function');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("and matcher is inverted with .not", function() {
|
||||||
|
it("should fail with an exception", function() {
|
||||||
|
var exception = catchException(function() {
|
||||||
|
match('not-a-function').not.toThrow();
|
||||||
|
});
|
||||||
|
expect(exception).toBeDefined();
|
||||||
|
expect(exception.message).toEqual('Actual is not a function');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe("when code block doesn not throw an exception", function() {
|
||||||
|
it("should fail (or pass when inverted with .not)", function() {
|
||||||
expect(match(function() {
|
expect(match(function() {
|
||||||
}).toThrow()).toEqual(false);
|
}).toThrow()).toEqual(false);
|
||||||
result = lastResult();
|
expect(lastResult().message).toEqual('Expected function to throw an exception.');
|
||||||
expect(result.message).toEqual('Expected function to throw an exception.');
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe(".not.matcher", function() {
|
describe(".not.matcher", function() {
|
||||||
|
|
Loading…
Reference in New Issue