Simplifying toThrow:

- It still supports no expected, which means that something was thrown
- Expected value is now tested via equality in order to pass

Adding toThrowError:
- toThrowError() passes if an Error type was thrown
- toThrowError(String) & toThrowError(RegExp) compare Expected to the Error message
- toThrowError(Error constructor) compares Expected to the constructor of what was thrown
- toThrowError(Error constructor, String) & toThrowError(Error constructor, RegExp) compares both the Error and the message

Also, equality now handles Errors, enforcing the message as part of the equality.
This commit is contained in:
Davis W. Frank
2013-06-03 09:24:43 -07:00
parent 9e31201f1a
commit 0ac497db6b
8 changed files with 611 additions and 188 deletions

View File

@@ -1,41 +1,16 @@
describe("toThrow", function() {
it("throw an error when the acutal is not a function ", function() {
it("throws an error when the actual is not a function", function() {
var matcher = j$.matchers.toThrow();
expect(function() {
matcher.compare({});
}).toThrow(new Error("Actual is not a Function"));
}).toThrow(new Error("Actual is not a Function")); // TODO: this needs to change for self-test
});
it("throws an error when the expected can't be turned into an exception", function() {
it("fails if actual does not throw", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
expect(function() {
matcher.compare(fn, 1);
}).toThrow(new Error("Expected cannot be treated as an exception."));
});
it("passes if the actual throws any exception", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception.");
});
it("fails if the actual does not throw an exception", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
return 0;
return true;
},
result;
@@ -45,107 +20,79 @@ describe("toThrow", function() {
expect(result.message).toEqual("Expected function to throw an exception.");
});
it("passes if the actual throws an exception with the expected message", function() {
it("passes if it throws but there is no expected", function() {
var util = {
equals: j$.createSpy('delegated-equal').andReturn(true)
},
matcher = j$.matchers.toThrow(util),
fn = function() {
throw 5;
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw.");
});
it("passes even if what is thrown is falsy", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
throw undefined;
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw.");
});
it("passes if what is thrown is equivalent to what is expected", function() {
var util = {
equals: j$.createSpy('delegated-equal').andReturn(true)
},
matcher = j$.matchers.toThrow(util),
fn = function() {
throw 5;
},
result;
result = matcher.compare(fn, 5);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw 5.");
});
it("fails if what is thrown is not equivalent to what is expected", function() {
var util = {
equals: j$.createSpy('delegated-equal').andReturn(false)
},
matcher = j$.matchers.toThrow(util),
fn = function() {
throw 5;
},
result;
result = matcher.compare(fn, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception \"foo\".");
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw 'foo'.");
});
it("fails if the actual throws an exception with a different message", function() {
var matcher = j$.matchers.toThrow(),
it("fails if what is thrown is not equivalent to undefined", function() {
var util = {
equals: j$.createSpy('delegated-equal').andReturn(false)
},
matcher = j$.matchers.toThrow(util),
fn = function() {
throw "foo";
throw 5;
},
result;
result = matcher.compare(fn, "bar");
result = matcher.compare(fn, void 0);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception \"bar\".");
});
it("passes if the actual throws an exception and matches the message of the expected exception", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn, new Error("foo"));
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception \"foo\".");
});
it("fails if the actual throws an exception and it does not match the message of the expected exception with a custom message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn, new Error("bar"));
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception \"bar\".");
});
it("passes if the actual throws an exception and the message matches the expected regular expression", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "a long message";
},
result;
result = matcher.compare(fn, /long/);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception matching /long/.");
});
it("fails if the actual throws an exception and the message does not match the expected regular expression", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "a long message";
},
result;
result = matcher.compare(fn, /short/);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception matching /short/.");
});
it("passes if the actual throws an exception with an undefined message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw void 0;
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception.");
});
it("passes if the actual throws an exception with an empty message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "";
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception.");
expect(result.message).toEqual("Expected function to throw undefined.");
});
});