admin管理员组文章数量:1417418
While writing QUnit tests I was suprised by the behaviour of 'throws'. Regarding the following code (/), could anyone please answer my questions:
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.");
});
Only the second test fails, although this would have been my natural choice of writing this test...
Questions:
1) Why do I have to use an inline function to surround my tested function?
2) Why does the last test not fail? 'func' does not throw any exception.
Would be grateful to read any explanation.
While writing QUnit tests I was suprised by the behaviour of 'throws'. Regarding the following code (http://jsfiddle/DuYAc/75/), could anyone please answer my questions:
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.");
});
Only the second test fails, although this would have been my natural choice of writing this test...
Questions:
1) Why do I have to use an inline function to surround my tested function?
2) Why does the last test not fail? 'func' does not throw any exception.
Would be grateful to read any explanation.
Share Improve this question asked Jan 13, 2014 at 16:06 user3190756user3190756 531 silver badge3 bronze badges1 Answer
Reset to default 71) Why do I have to use an inline function to surround my tested function?
You don't. When you write throws(subfunc(), [...])
, subfunc()
is evaluated first. As subfunc()
throws outside the throws
function, the test fails immediately. In order to fix it, you have to pass throws
a function. function(){subfunc()}
works, but so does subfunc
:
test("This works", function () {
throws(subfunc, "Must throw error to pass.");
});
2) Why does the last test not fail? 'func' does not throw any exception.
For the same reason. func()
is evaluated first. As there is no explicit return
statement, it returns undefined
. Then, throws
tries to call undefined
. As undefined
is not callable, an exception is thrown and the test passes.
test("This doesn't work", function () {
throws(func, "Must throw error to pass.");
});
本文标签: javascriptUnderstanding QUnit exception testingStack Overflow
版权声明:本文标题:javascript - Understanding QUnit exception testing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745265740a2650587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论