正如我的用户名所暗示的,我是 Node.js 的新手。我真的很喜欢很多想法。我只是想开始学习它。我希望了解的一件事是如何使用 Node.js 设置测试驱动环境。
目前,我有一个名为 ToTest.min.js 的基本 javascript 文件。该文件定义了一个名为 ToTest 的类,它公开了一个名为 getItems 的方法。为了测试这种方法,我在 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);
});
});
我通过以下命令行从命令行执行此文件:
mocha Test.js
执行时,我看到以下输出:
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
如何让记者工作,以便更好地格式化单元测试的结果?我试过
mocha Test.js -R dot
我收到了同样的结果。