Fix up my refactoring mistake in toThrowError

strengthen the associated tests to protect against it in the future
This commit is contained in:
Sheel Choksi
2013-06-04 00:25:12 -07:00
parent b87eb240b3
commit 600be098af
3 changed files with 16 additions and 6 deletions

View File

@@ -2107,7 +2107,7 @@ getJasmineRequireObj().toThrowError = function() {
} }
if (errorType && message) { 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 + "\"."); return pass("Expected function not to throw Error with message \"" + message + "\".");
} else { } else {
return fail("Expected function to throw Error with message \"" + message + "\"."); return fail("Expected function to throw Error with message \"" + message + "\".");
@@ -2172,7 +2172,7 @@ getJasmineRequireObj().toThrowError = function() {
regexp = expected; regexp = expected;
} else if (typeof expected == "string") { } else if (typeof expected == "string") {
message = expected; message = expected;
} else if (typeof expected == "function" && new expected() instanceof Error) { } else if (checkForAnErrorType(expected)) {
errorType = expected; errorType = expected;
} }
@@ -2180,7 +2180,7 @@ getJasmineRequireObj().toThrowError = function() {
throw new Error("Expected is not an Error, string, or RegExp."); throw new Error("Expected is not an Error, string, or RegExp.");
} }
} else { } else {
if (typeof arguments[1] == "function" && new arguments[1]() instanceof Error) { if (checkForAnErrorType(arguments[1])) {
errorType = arguments[1]; errorType = arguments[1];
} else { } else {
throw new Error("Expected error type is not an Error."); 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;
}
} }
}; };
} }

View File

@@ -25,7 +25,7 @@ describe("toThrowError", function() {
}; };
expect(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 }).toThrow(new Error("Expected error type is not an Error.")); // TODO: this needs to change for self-test
}); });

View File

@@ -124,14 +124,14 @@ getJasmineRequireObj().toThrowError = function() {
} }
function checkForAnErrorType(type) { function checkForAnErrorType(type) {
if (typeof expected == "function") { if (typeof type !== "function") {
return false; return false;
} }
var Surrogate = function() {}; var Surrogate = function() {};
Surrogate.prototype = type.prototype; Surrogate.prototype = type.prototype;
return (new Surrogate()) instanceof Error; return (new Surrogate()) instanceof Error;
}; }
} }
}; };
} }