QUnit テストを書いているときに、「スロー」の動作に驚きました。次のコード ( 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.");
});
失敗するのは 2 番目のテストだけですが、これはこのテストを作成する際の私の自然な選択でした...
質問:
1) テストした関数を囲むためにインライン関数を使用する必要があるのはなぜですか?
2) 最後のテストが失敗しないのはなぜですか? 'func' は例外をスローしません。
説明を読んでいただければ幸いです。