我试图检查一个函数是否会抛出错误,并做了如下操作:
define([
'doh/runner',
'app/Obj'
], function(
doh,
Obj
){
doh.register('Test Obj exception', [
function () {
try {
new Obj(); // should throw error
} catch(e) {
doh.t(e, 'should give an error if no parameters given');
}
}
]);Obj.js文件:
...
constructor: function (args){
if (!args) { throw 'Error' }
...
}
...但也许在国防部有什么正确的方法来解决这个问题呢?有人能解释一下吗?谢谢
发布于 2014-03-27 17:03:44
您想要doh.assertError()
示例:
doh.assertError(TypeError, this.field, "setValue",
[{
CreatedOn: "March 10th, 2014"
}],
"setValue() on an invalid format should throw a TypeError");发布于 2013-04-17 19:01:35
这个例子测试显示DOH捕获并正确显示错误。
这个要旨是测试,包含以下代码:
var Obj = function () {
if (arguments.length < 1) {
throw 'Error - There are ' + arguments.length + ' arguments';
}
};
define(["doh/runner"], function(doh){
var tests = [
function () {
new Obj(); // wrong call
}
];
doh.register('Test Obj exception', tests);
});屏幕快照显示了1错误,以及抛出的Error中的错误消息:

https://stackoverflow.com/questions/16062265
复制相似问题