2012-11-27 48 views
6

我是node.js和單元測試框架Mocha的新手,但我已經在cloud9 IDE中創建了幾個測試,以瞭解它是如何工作的。代碼如下所示:Cloud9中的Mocha測試中的「未聲明的變量」警告

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

測試工作,如果我在控制檯輸入摩卡,但IDE顯示在「描述」和「它」是行警告,因爲它說,該變量尚未聲明(「未聲明的變量」)。

我不知道該怎麼做這些測試來避免警告。

謝謝。

回答

0

這是因爲mocha「可執行文件」在使用摩卡功能(describeit)所需的require中包裝您的測試。請在您的node_modules/mocha/bin目錄中查看mocha_mocha

另一方面cloud9嘗試使用純可執行文件node來解析所有符號,因此您必須手動將所有內容都保存到require

+0

那麼我需要什麼手工?因爲我試圖做var mocha = require(「mocha」);並將代碼更改爲mocha.describe ...並且警告消失,但是在執行摩卡時失敗。 – Juanillo

+0

@Juanillo我不確定這有一個簡單的解決方法。您可以強制cloud9運行您的摩卡測試,但這並不能解決未聲明的變量問題。 – soulcheck

+0

好的,謝謝,我對此很好奇,因爲我是這個平臺的新手,但我想知道有經驗的程序員是否在這種情況下做了些什麼。但是,似乎其他實際項目使用了類似代碼的Mocha。也許他們只是在cloud9工作時忽略這些警告,但我真的很驚訝在真正嚴肅的項目中可以給出警告。 – Juanillo

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); 
    }) 
    }) 
}) 
+0

我認爲正確的語法是'/ * global在* /'之前描述的。另見http://stackoverflow.com/a/19572515/209727 –