admin管理员组文章数量:1208153
QUnit has an assertion for testing that a function raises an exception (QUnit/raises). Is it possible -- using QUnit -- to assert that a function does not raise an exception.
I realize that it is possible to test it like in the following code:
try {
theTest();
ok(true);
} catch (e) {
ok(false, "Expected to succeed");
}
But I think it ought to be possible using QUnit. Any clues?
QUnit has an assertion for testing that a function raises an exception (QUnit/raises). Is it possible -- using QUnit -- to assert that a function does not raise an exception.
I realize that it is possible to test it like in the following code:
try {
theTest();
ok(true);
} catch (e) {
ok(false, "Expected to succeed");
}
But I think it ought to be possible using QUnit. Any clues?
Share Improve this question asked Mar 22, 2012 at 12:34 anissenanissen 2513 silver badges8 bronze badges 1- check this stackoverflow.com/questions/10190392/… it can help – hanane Commented Mar 30, 2014 at 20:44
2 Answers
Reset to default 18There is no such method in qunit
However, if you just write the following code which is much shorter, you will obtain the same result with additionnal benefits
theTest();
ok(true, "My function does not crash");
1/ If the code of a test raises an exception, qunit will mark the test as failed.
2/ If you check the "no try/catch" checkbox, you will be able to debug where the exception was thrown, which is not the case with your try/catch
I had the same issue as you mentioned in the comment whereby my test which tests no Error
is thrown would stop "badly" showing a badly formatted Died on test #1
message without any useful information.
I ended up using a mix of both; raises()
for one test and try/catch
for the other.
I used raises() for the test which tests that an Error
is thrown, similar to this:
test("When myFunction() is called with a invalid instance Then Error is thrown", function () {
// Arrange
var testInstance = {};
// Act
raises(function() {
myFunction(testInstance);
}, Error, "myFunction() should throw an Error");
// Assert
// raises() does assertion
});
If the above throws an Error
all is fine and if not a nice formatted message is displayed, similar to this:
myFunction() should throw Error
Result: No exception was thrown.
I then used try/catch
for the tests which have to ensure no Error
is thrown, similar to this:
test("When myFunction() is called with a valid instance Then no Error is thrown", function () {
// Arrange
var testInstance = new myObject();
var result;
// Act
try {
myFunction(testInstance);
result = true;
} catch(error) {
result = false;
}
// Assert
ok(result, "myFunction() should not throw an Error");
});
If the above throws no Error
all is fine and if an Error
is thrown a nice formatted message is displayed, similar to this:
myFunction() should not throw an Error
Source: ...
本文标签: javascriptHow to assert that a function does not raise an exceptionStack Overflow
版权声明:本文标题:javascript - How to assert that a function does not raise an exception - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738763049a2111075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论