0

我目前正在学习如何使用 Tape 进行单元测试。我已经能够验证我的测试中是否引发了错误。但是,我们如何验证抛出错误的消息是否等于预期的消息?

示例单元测试:

var test = require('tape'),
    ExampleObject = require('/path/to/ExampleObject');

test("Pass invalid argument to function", function(assert){
    assert.throws(function(){
        new ExampleObject(undefined, "validParameter")
    }, TypeError, "Should throw TypeError for firstParam");

    assert.end();
});

示例对象:

function ExampleObject(param1, param2){
    if(typeof param1 !== 'string') {
      throw new TypeError('ExampleObject - typeof for param1 should be string');
    }
    if(typeof param2 !== 'string') {
      throw new TypeError('ExampleObject - typeof for param2 should be string');
    }

    /*
    / do stuff
    */
};

提前致谢!

4

1 回答 1

0

我在 Github 存储库中打开了一个问题。目前,.throws() 不支持同时检查错误类型和消息。

https://github.com/substack/tape/issues/237

于 2016-01-21T17:41:20.220 回答