Qunit异常测试

时间:2017-12-01 22:40:42

标签: qunit

通过查看this question,我找不到当前问题的答案:

我有一个自定义异常(根据this guide设计):

let StrictOverwrite_destination_error = function(message) {
    var error = new Error(message);
    error.name = "utils_misc.StrictOverwrite_destination_error";    
    throw error;
};

我从函数strictOverwrite抛出:

function strictOverwrite ( destination, source, debug ) {

    // lots of code snipped

        else if ( !destination.hasOwnProperty( property ) && source.hasOwnProperty( property ) )
        {
            throw new StrictOverwrite_destination_error(
                "source.property exists, but destination.property doesn't" );
        }

    // more code snipped

    return destination;
}

错误和函数都在我的模块utils_misc中定义,尽管我的自定义异常定义不是从命名空间导出的。

这是我测试strictOverwrite函数抛出异常的方法:

assert.throws(
    function() {
        utils_misc.strictOverwrite(c, c_dominator, false);
    },
    function(error) {
        return error.name === "utils_misc.StrictOverwrite_destination_error";
    }
);
但是,唉,Qunit不满意:

Expected:   

function( a ){
  [code]
}

Result:     

{
  "message": "property is not defined",
  "name": "ReferenceError"
}

Diff:   

function( a ){
  [code]{
  "message": "property is not defined",
  "name": "ReferenceError"
}

除了整件事不起作用外,令我困惑的一件事是Qunit如何获得这条消息:"property is not defined"。这不完全是我的自定义StrictOverwrite_destination_error例外产生的消息。

1 个答案:

答案 0 :(得分:0)

此问题中编写的测试代码运行正常。问题出在正在测试的应用程序代码中。