2016-05-23 49 views
0

我嘗試做與摩卡&柴單元的NodeJS測試...摩卡/柴to.not.throw(類型錯誤)奇怪的行爲

我的第一個測試套件來驗證,如果一個構造函數返回或不根據參數類型返回TypeError。

/* ./test/entity/Point.js */ 

"use strict"; 

var expect = require("chai").expect; 
var Point = require("./../entity/Point"); 

describe("Point entity", function() { 
    it("x parameter must not be string", function() { 
     var fn = function() { var p = new Point("5", 40); }; 
     expect(fn).to.throw(TypeError); 
    }); 

    it("y parameter must not be string", function() { 
     var fn = function() { var p = new Point(5, "40"); }; 
     expect(fn).to.throw(TypeError); 
    }); 

    it("x and y parameters must be number", function() { 
     var fn = function() { var p = new Point(5, 40); }; 
     expect(fn).to.not.throw(TypeError); 
    }); 
}); 

,我測試的構造函數:

/*./entity/Point.js */ 

"use strict"; 

function Point(x, y) { 
    // Type validation 
    if(x && !(typeof x === "number")) { 
     throw new TypeError("x is expected to be a number"); 
    } 
    if(y && !(typeof y === "number")) { 
     throw new TypeError("y is expected to be a number"); 
    } 

    // instantiation 
    this.x = x; 
    this.y = y; 
} 

module.exports = Point; 

第一次測試就可以了。 第二項測試是可以的。 但第三次測試給我一個奇怪的斷言錯誤:如果我使用柴或的NodeJS斷言模塊

 
    Point entity 
    V x parameter must not be string 
    V y parameter must not be string 
    1) x and y parameters must be number 


    2 passing (55ms) 
    1 failing 

    1) Point entity x and y parameters must be number: 
    AssertionError: Got unwanted exception (TypeError).. 
     at _throws (assert.js:341:5) 
     at Function.assert.doesNotThrow (assert.js:359:3) 
     at Context. (C:\Users\Cedric\Documents\NodeProjects\SqueezeParking\test\entity\Point.js:19:16) 
     at callFn (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:326:21) 
     at Test.Runnable.run (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:319:7) 
     at Runner.runTest (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:422:10) 
     at C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:528:12 
     at next (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:342:14) 
     at C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:352:7 
     at next (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14) 
     at Immediate._onImmediate (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:320:5) 

 
    Point entity 
    V x parameter must not be string 
    V y parameter must not be string 
    1) x and y parameters must be number 


    2 passing (67ms) 
    1 failing 

    1) Point entity x and y parameters must be number: 
    AssertionError: expected [Function] to not throw 'TypeError' but 'TypeError: Point is not a function' was thrown 

     at Context. (C:\Users\Cedric\Documents\NodeProjects\SqueezeParking\test\entity\Point.js:19:32) 
     at callFn (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:326:21) 
     at Test.Runnable.run (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:319:7) 
     at Runner.runTest (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:422:10) 
     at C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:528:12 
     at next (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:342:14) 
     at C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:352:7 
     at next (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14) 
     at Immediate._onImmediate (C:\Users\Cedric\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:320:5) 

如果我使用的NodeJS斷言模塊,而不是柴,我也得到一個斷言錯誤摩卡外,第三次測試不亂扔類型錯誤:

/*./server.js */ 

var expect = require("chai").expect; 
var Point = require("./entity/Point"); 
var fn = function() { 
    var p = new Point(10, 20); 
} 
expect(fn).to.not.throw(TypeError); 

看來,這種奇怪的類型錯誤來自摩卡,但我不明白爲什麼,以及如何... 有人有想法嗎?

回答

1

您的測試代碼將自己導入爲模塊。爲了說明,在./test/entity/Point.js這個開始改變你的代碼:

exports.foo = "foo"; 
var expect = require("chai").expect; 
var Point = require("./../entity/Point"); 
console.log(Point); 

,你會得到輸出:

路徑使用要求要測試的代碼
{ foo: 'foo' } 
[... followed by the tests passing and failing ...] 

變化var Point = require("../../entity/Point");並且您的測試將通過。

+0

哈哈,nice catch = D – robertklep

+0

天啊!我爲自己感到羞恥! 感謝您的幫助@Louis。 –