0
を必要とするとき、エラーメッセージは、私はユニットテストに新しい、私はちょうどのNode.js:ユニットテスト、機能が特定の値
間違った値の関数を呼び出すときに、特定のエラーが発生することをユニットテストで嫌うだけです。
機能:
module.exports.parseOptions = function(opts){
if(utils.isFalsy(opts)){
throw new Error("Options is undefined");
}
else if(!utils.isObject(opts)){
throw new Error("Options parameter must be an object");
}
else{
// Path is mandatory for this package, throw error if undefined
if(!opts.path){
throw new Error("Path for static files is undefined");
}
.......
}
}
ユニットテストファイル:
var expect = require("expect");
var helper = require("./../lib/helper.js");
describe("helper functions", function(){
it("should parse options", function(){
//This process works fine
expect(function(){
helper.parseOptions();
}).toThrow("Options is undefined");
//This process fails for some reason
expect(function(){
helper.parseOptions([]);
}).toThrow("Options parameter must be an object");
});
})
示してどのようなターミナル:
helper functions
1) should parse options
0 passing (9ms)
1 failing
1) helper functions should parse options:
Error: Expected [Function] to throw 'Options parameter must be an object'
at assert (node_modules/expect/lib/assert.js:29:9)
at Expectation.toThrow (node_modules/expect/lib/Expectation.js:104:28)
at Context.<anonymous> (test/test-driven-development.js:12:6)
私はユニットテストをしたいと思い、それは本当にどのように動作するかを理解するために苦労していますすべての種類のエラーを期待値と同様に検証するには、他に便利なツールがあれば教えてください。