Jua*_*llo 6 javascript unit-testing mocha.js node.js cloud9-ide
我是node.js和用于单元测试的框架Mocha的新手,但是我在cloud9 IDE中创建了几个测试,看看它是如何工作的.代码如下所示:
var assert = require("assert");
require("should");
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
});
});
});
describe('Array', function(){
describe('#indexOf()', function(){
it('should return the index when the value is present', function(){
assert.equal(1, [1,2,3].indexOf(2));
assert.equal(0, [1,2,3].indexOf(1));
assert.equal(2, [1,2,3].indexOf(3));
});
});
});
Run Code Online (Sandbox Code Playgroud)
如果我在控制台中键入mocha,则测试会起作用,但IDE会在"describe"和"it"的行中显示警告,因为它表示该变量尚未声明("未声明的变量").
我想知道我应该怎么做这些测试以避免警告.
谢谢.
这是因为mocha“可执行文件”将您的测试包装在require使用 mocha 函数(describe、 和it)所需的 s 中。查看您的目录中的mocha和。_mochanode_modules/mocha/bin
另一方面,cloud9尝试使用纯可执行文件解析所有符号node,因此您必须require手动完成所有操作。