6

我是 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));
    });
  });
});

如果我在控制台中键入 mocha,测试将起作用,但 IDE 在“描述”和“它”的行中显示警告,因为它表示该变量尚未声明(“未声明的变量”)。

我想知道我应该怎么做这些测试以避免警告。

谢谢。

4

2 回答 2

2

在 cloud9 中,您可以在文件顶部添加全局提示作为注释,它将删除警告。例如

**/* global describe it before */**

var expect = require('chai').expect;


describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return -1 when the value is not present', function(){
        expect(true).to.equal(true);
    })
  })
})
于 2014-06-27T16:00:11.677 回答
0

这是因为mocha“可执行文件”将您的测试包装在requires 中,以使用 mocha 函数(describeit)。查看mocha_mocha在您的node_modules/mocha/bin目录中。

另一方面,cloud9尝试使用纯node可执行文件解析所有符号,因此您必须require手动处理所有内容。

于 2012-11-28T09:53:08.287 回答