Fix up my refactoring mistake in toThrowError
strengthen the associated tests to protect against it in the future
This commit is contained in:
@@ -2107,7 +2107,7 @@ getJasmineRequireObj().toThrowError = function() {
|
||||
}
|
||||
|
||||
if (errorType && message) {
|
||||
if (util.equals(thrown, new errorType(message))) {
|
||||
if (thrown.constructor == errorType && util.equals(thrown.message, message)) {
|
||||
return pass("Expected function not to throw Error with message \"" + message + "\".");
|
||||
} else {
|
||||
return fail("Expected function to throw Error with message \"" + message + "\".");
|
||||
@@ -2172,7 +2172,7 @@ getJasmineRequireObj().toThrowError = function() {
|
||||
regexp = expected;
|
||||
} else if (typeof expected == "string") {
|
||||
message = expected;
|
||||
} else if (typeof expected == "function" && new expected() instanceof Error) {
|
||||
} else if (checkForAnErrorType(expected)) {
|
||||
errorType = expected;
|
||||
}
|
||||
|
||||
@@ -2180,7 +2180,7 @@ getJasmineRequireObj().toThrowError = function() {
|
||||
throw new Error("Expected is not an Error, string, or RegExp.");
|
||||
}
|
||||
} else {
|
||||
if (typeof arguments[1] == "function" && new arguments[1]() instanceof Error) {
|
||||
if (checkForAnErrorType(arguments[1])) {
|
||||
errorType = arguments[1];
|
||||
} else {
|
||||
throw new Error("Expected error type is not an Error.");
|
||||
@@ -2195,6 +2195,16 @@ getJasmineRequireObj().toThrowError = function() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function checkForAnErrorType(type) {
|
||||
if (typeof type !== "function") {
|
||||
return false;
|
||||
}
|
||||
|
||||
var Surrogate = function() {};
|
||||
Surrogate.prototype = type.prototype;
|
||||
return (new Surrogate()) instanceof Error;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ describe("toThrowError", function() {
|
||||
};
|
||||
|
||||
expect(function() {
|
||||
matcher.compare(fn, "string", "foo");
|
||||
matcher.compare(fn, void 0, "foo");
|
||||
}).toThrow(new Error("Expected error type is not an Error.")); // TODO: this needs to change for self-test
|
||||
});
|
||||
|
||||
|
||||
@@ -124,14 +124,14 @@ getJasmineRequireObj().toThrowError = function() {
|
||||
}
|
||||
|
||||
function checkForAnErrorType(type) {
|
||||
if (typeof expected == "function") {
|
||||
if (typeof type !== "function") {
|
||||
return false;
|
||||
}
|
||||
|
||||
var Surrogate = function() {};
|
||||
Surrogate.prototype = type.prototype;
|
||||
return (new Surrogate()) instanceof Error;
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user