我正在使用Backbone和Backbone.Marionette編寫單頁JavaScript應用程序。我使用AMD模塊和RequireJS來幫助組織我的代碼並管理依賴關係。我也使用Mocha作爲TDD/BDD的測試框架。如何在使用RequireJS和Mocha使用Squire.js時避免引入全局泄漏?
一切工作正常,直到我想引入使用Sinon.JS的存根,嘲笑和間諜。經過大量搜索後,我偶然發現了一個test frameworks in the RequireJS wiki和Squire.js的頁面,這看起來很適合我的需求。但是,當我嘗試使用Squire.js加載模塊時,Mocha突然報告模塊依賴關係的全局泄漏。如果我使用Require.JS直接加載模塊,則沒有報告泄漏。
例如,下面的測試代碼不會導致摩卡報告任何泄漏:
define(['app/app'], function(app) {
describe('App', function() {
it('Should define a \'header\' region', function() {
expect(app.headerRegion).to.exist;
});
it('Should define a \'main\' region', function() {
expect(app.mainRegion).to.exist;
});
});
return {
name: "App"
};
});
然而,轉換代碼,以使用Squire.js如下導致摩卡報告的jQuery,骨幹泄漏,並木偶(app.js的依賴關係):
define(['Squire'], function(Squire) {
describe('App', function() {
var testContext = {};
beforeEach(function(done) {
testContext.injector = new Squire();
testContext.injector.require(['app/app'], function(app) {
testContext.app = app;
done();
});
});
it('Should define a \'header\' region', function() {
expect(testContext.app.headerRegion).to.exist;
});
it('Should define a \'main\' region', function() {
expect(testContext.app.mainRegion).to.exist;
});
});
return {
name: "App"
};
});
我在做什麼錯了?我完全不理解Mocha並沒有用RequireJS報告泄漏,而是用Squire.js報告。我還嘗試了一些其他的解決方案,我在另一個StackOverflow question on mocking RequireJS dependencies中找到了,比如Squire.js之前的自定義函數和testr.js,並且有類似的結果。迄今爲止,我一直無法找到一個使用Mocha,RequireJS和Sinon.JS的例子。
我已經placed my current code base on GitHub萬一有一些重要的信息,我遺漏了或什麼的。有關測試可在test\spec\test.app.js中找到。
任何幫助,非常感謝。我非常想通過我的測試設置和實際上在我的應用程序工作。提前致謝。