0
我目前正在学习如何使用磁带进行单元测试。我已经能够验证在我的测试中发生了错误。但是,我们如何验证使用Error引发的消息等于预期的消息?如何使用磁带验证错误消息?
实施例的单元测试:
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();
});
ExampleObject:
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
*/
};
提前感谢!