在编写QUnit测试时,我对'throws'的行为感到惊讶。 关于下面的代码(http://jsfiddle.net/DuYAc/75/),请问有人能回答我的问题吗:
function subfunc() {
throw "subfunc error";
}
function func() {
try {
subfunc();
} catch (e) {}
}
test("official cookbook example", function () {
throws(function () {
throw "error";
}, "Must throw error to pass.");
});
test("Would expect this to work", function () {
throws(subfunc(), "Must throw error to pass.");
});
test("Why do I need this encapsulation?", function () {
throws(function(){subfunc()}, "Must throw error to pass.");
});
test("Would expect this to fail, because func does not throw any exception.", function () {
throws(func(), "Must throw error to pass.");
});
只有第二个测试失败,尽管这可能是我编写此测试的自然选择...
问题:
1)为什么我必须使用内联函数来包围我的被测函数?
2)为什么最后一个测试没有失败?'func'没有抛出任何异常。
很感谢能阅读到任何解释。