简体   繁体   English

在Node.js上将Reporters与Mocha结合使用

[英]Using Reporters with Mocha on Node.js

As my username implies, I'm brand new to Node. 顾名思义,我是Node的新手。 I really like a lot of the ideas. 我真的很喜欢很多想法。 I'm just trying to get started with learning it. 我只是想开始学习它。 One of the things I'm looking to understand is how to setup a test driven environment with Node. 我想了解的一件事是如何使用Node设置测试驱动的环境。

Currently, I have a basic javascript file called ToTest.min.js. 当前,我有一个名为ToTest.min.js的基本javascript文件。 This file defines a class called ToTest, which exposes a method called getItems. 该文件定义了一个名为ToTest的类,该类公开了一个名为getItems的方法。 To test this method, I've written the following code in Test.js: 为了测试此方法,我在Test.js中编写了以下代码:

var should = require("should");
var toTest = require("../ToTest.min.js");

describe('ToTest.js', function () {
    describe('test1', function () {
        var t = new ToTest();
        var items = t.getItems();

        console.log("Result: " + items.length);
        t.should.have.length(1);
    });
});

I execute this file from the command line via: 我从命令行通过以下方式执行此文件:

mocha Test.js

When its executed, I see the following output: 执行后,我看到以下输出:

Result: 0

C:\Projects\MyProject\node_modules\sh
ould\lib\should.js:130
    throw err;
          ^
AssertionError: expected [] to have a length of 1 but got 0
    at Object.Assertion.length (C:\Projects\MyProject\node_modules\should\lib\should.js:434:10)
    at Suite.<anonymous> (C:\Projects\MyProject\Test.js:9:27)
    at context.describe.context.context (C:\Users\Myself\AppData\Roaming\npm\n
ode_modules\mocha\lib\interfaces\bdd.js:72:10)
    at Suite.<anonymous> (C:\Projects\MyProject\Test.js:5:5)
    at context.describe.context.context (C:\Users\Myself\AppData\Roaming\npm\n
ode_modules\mocha\lib\interfaces\bdd.js:72:10)
    at Object.<anonymous> (C:\Projects\MyProject\Test.js:4:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at C:\Users\Myself\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:152
:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Users\Myself\AppData\Roaming\npm\node_modules\mocha
\lib\mocha.js:149:14)
    at Mocha.run (C:\Users\Myself\AppData\Roaming\npm\node_modules\mocha\lib\m
ocha.js:306:31)
    at Object.<anonymous> (C:\Users\Myself\AppData\Roaming\npm\node_modules\mo
cha\bin\_mocha:339:7)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

How do I get a reporter working so that the results of the unit tests are formatted better? 如何让记者工作,以便更好地格式化单元测试的结果? I tried 我试过了

mocha Test.js -R dot

and I received the same result. 我收到了相同的结果。

Ah, you need to use the it function for actual tests, not describe . 嗯,您需要将it函数用于实际测试,而不要describe

var should = require("should");
var toTest = require("../ToTest.min.js");

describe('ToTest.js', function () {
    it('test1', function () {
        var t = new ToTest();
        var items = t.getItems();

        console.log("Result: " + items.length);
        t.should.have.length(1);
    });
});

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

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