2016-08-16 7 views
0

を必要とするとき、エラーメッセージは、私はユニットテストに新しい、私はちょうどのNode.js:ユニットテスト、機能が特定の値

  • expect
  • mocha.js
  • mocha.jsを使用して練習のためのランダムなテストを行うと、 expectたのです期待

間違った値の関数を呼び出すときに、特定のエラーが発生することをユニットテストで嫌うだけです。

機能:

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) 

私はユニットテストをしたいと思い、それは本当にどのように動作するかを理解するために苦労していますすべての種類のエラーを期待値と同様に検証するには、他に便利なツールがあれば教えてください。

答えて

0

私は、テストが失敗した理由を見つけたが、それは[]値がfalsyであることを意味し、falsyですので、関数は値が未定義であるというエラーをスローした場合、検証された機能に送られたパラメータのように見える

module.exports.parseOptions = function(opts){ 
    if(utils.isFalsy(opts)){ 
     throw new Error("Options is undefined"); 
    } 
     ....... 
} 

:私はから変更しなければならなかった

module.exports.parseOptions = function(opts){ 
    if(typeof opts == "undefined"){ 
     throw new Error("Options is undefined"); 
    } 
     ....... 
} 
関連する問題