JS测试驱动程序的正确语法“;assertException”;

Correct syntax for JS Test Driver "assertException"?

本文关键字:assertException 语法 测试驱动程序 JS      更新时间:2023-09-26

我正试图弄清楚如何正确使用JS测试驱动程序的assertException方法。从谷歌的文档来看,它应该是:assertException([msg],callback,error(。然而,无论我做什么,我总是得到这个测试的[ERROR](而不是[PASS],因为有一个例外(:

ComponentTest.prototype.testEnforceUniqueComponentIds = function() {
    assertException(function(){
        this.component = new myNamespace.component(this.testComponentSettings);
    });
}

在我的src JS:中

myNamespace.component = function(params){
if (typeof params.selector == 'undefined'){
    throw new Error('no selector provided');
}
this.settings = jQuery.extend({}, {
    id              : 'defaultComponentId',
    type            : 'defaultComponentType',
    selector    : 'body',
    isUnique    : false
}, params);
if(typeof(myNamespace.componentRegistry[params.id]) === "undefined"){
    myNamespace.registerComponent(this);
} else {
    throw new Error('non-unique component id provided');
}
}

以下是js测试驱动程序的输出:

C:'xampp2'htdocs'myNamespace'v2>java -jar JsTestDriver.jar --tests all --verbose
[PASSED] ComponentTest.testInit
  [LOG] setUp
  [LOG] tearDown
[PASSED] ComponentTest.testDestroy
  [LOG] setUp
  [LOG] tearDown
[ERROR] ComponentTest.testEnforceUniqueComponentIds
  [LOG] setUp
Total 3 tests (Passed: 2; Fails: 0; Errors: 1) (1.00 ms)
  Firefox 7.0.1: Run 3 tests (Passed: 2; Fails: 0; Errors 1) (1.00 ms)
    ComponentTest.testEnforceUniqueComponentIds error (1.00 ms):

就我而言,我不知道如何让assertException回调中的代码真正抛出异常而不会在测试中导致错误——这不是应该发生的吗?

如有任何帮助,我们将不胜感激。

assertException(function(){
    this.component = new myNamespace.component(this.testComponentSettings);
}, "Error");

error参数应为抛出错误的error.name(如"ReferenceError"、"TypeError"等(。