1
我已經創建了簡單的Mocha測試。當使用Node「assert」模塊時,它完美工作。我運行它的命令行(摩卡安裝爲一個全球性的節點模塊):Chai出口在摩卡測試中找不到
$ mocha myTest.js
․
1 test complete (6 ms)
腳本是這樣的:
var assert = require("assert")
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));
})
})
})
嗯,我想補充的,而不是斷言庫柴。我先安裝它:
npm install chai
因此,目錄node_modules已經在我的項目中創建。太好了。然後,我改變了腳本使用柴:
var chai = require("chai");
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
[1,2,3].indexOf(5).should.equal(-1);
expect([1,2,3].indexOf(5)).to.equal(-1);
assert.equal([1,2,3].indexOf(5),-1);
})
})
});
它不工作,摩卡測試失敗,類型錯誤:
TypeError: Cannot call method 'equal' of undefined
我認爲柴沒有定義應該因此它是不確定的。
這怎麼可能?
我該如何讓我的測試與Chai一起運行?我試圖在全球範圍內安裝Chai而沒有任何效果。我也用-r chai
運行腳本,但也沒有效果。
顯然,Chai模塊被加載,但沒有定義變量(Object.prototype屬性)。我怎樣才能解決這個問題?
感謝您的幫助,男人! – 2013-05-03 10:12:59
我是否必須始終手動要求? – 2015-12-13 22:33:17
是的。這是CommonJS社區做出的設計決定。顯式聲明的依賴關係,即使是冗長的,也優於隱式依賴關係。 – 2015-12-14 14:00:20