繁体   English   中英

摩卡/柴to.not.throw(TypeError)奇怪的行为

[英]Mocha/Chai to.not.throw(TypeError) strange behavior

我尝试使用Mocha&Chai在NodeJ上进行单元测试。

我的第一个测试套件是根据参数类型来验证构造函数是否返回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;

第一次测试还可以。 第二次测试还可以。 但是第三次​​测试向我发送了一个奇怪的断言错误:

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断言模块而不是Chai,也会收到断言错误:

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)

如果我在Mocha之外使用Chai或NodeJs断言模块,则第三次测试不要抛出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);

看来这个奇怪的TypeError来自Mocha,但是我不明白为什么以及如何...有人有想法吗?

您的测试代码将自己导入为模块。 为了说明,请将您在./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"); 您的测试就会通过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM