2016-05-23 12 views
0

Ich versuche auf NodeJS Unit-Tests zu tun mit Mokka & Chai ...Mokka/Chai to.not.throw (Typeerror) seltsames Verhalten

Meine ersten Testsuite ist, wenn eine Konstruktorfunktion Rückkehr zu verifizieren oder nicht Gibt TypeError abhängig vom Parametertyp zurück.

/* ./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); 
    }); 
}); 

Der Konstruktor-Funktion, die ich testen:

/*./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; 

Erster Test ist in Ordnung. Zweiter Test ist in Ordnung. Aber dritten Test mir eine seltsame Behauptung Fehler senden:

 
    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) 

Wenn ich NodeJS behaupten Modul statt Chai verwendet, habe ich auch eine Assertion Fehler angezeigt:

 
    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) 

Wenn ich Chai verwenden oder NodeJS Modul behaupten außerhalb Mokka, nicht werfen dritten Test Typeerror:

/*./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); 

Es scheint, dass diese seltsam aus Mokka Typeerror kommen, aber ich verstehe nicht, warum und wie ... Hat jemand eine Idee?

Antwort

1

Ihr Testcode importiert sich selbst als Modul.

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

Und Sie werden die Ausgabe::

{ foo: 'foo' } 
[... followed by the tests passing and failing ...] 

Ändern Sie den Pfad der Code, den Sie testen wollen, verlangen verwenden zu Beginn der ./test/entity/Point.js dazu zu veranschaulichen, können Sie Code ändern var Point = require("../../entity/Point"); und Ihre Tests werden bestanden.

+0

Haha, schöner Fang = D – robertklep

+0

Oh mein Gott! Ich schäme mich meiner selbst! Danke für Ihre Hilfe @Louis. –