2015-06-05 28 views
0

我正在使用可嵌入JavaScript引擎的遊戲引擎(Atomic Game Engine)中使用Duktape。這允許CommonJS模塊加載,所以我有一個像以下模塊:使用Chutzpah來測試duktape中使用的CommonJS模塊

require("Scripts/LumaJS/Interface"); 

// Interface to be implemented by state objects 
var IState = new Interface({ 
    optional: ["onEnter", "onExit", "onUpdate"], 
    reserved: ["label"] 
}); 

StateMachine = function(onStateChanged) { 
    this.states = {}; 
    this.currentState = null; 
    this.onStateChanged = onStateChanged; 
    this.log = false; 
} 

StateMachine.prototype.addState = function (label, stateOrOnEnter, onExit, onUpdate) { 
    // Support objects or individual functions 
    var state; 
    if (!stateOrOnEnter || 
     typeof (stateOrOnEnter) == "function") { 
     state = { 
      onEnter: stateOrOnEnter, 
      onExit: onExit, 
      onUpdate: onUpdate 
     }; 
    } else { 
     state = stateOrOnEnter; 
    } 

    // Validate and add 
    IState.throwIfNotImplementedBy(state); 
    state.label = label; 
    this.states[label] = state; 
} 

StateMachine.prototype.getCurrentStateLabel = function() { 
    return this.currentState ? this.currentState.label : null; 
} 

// Many more class functions.. 

exports.create = function (onStateChanged) { 
    return new StateMachine(onStateChanged); 
} 

我得放肆設立單元測試VS社區版(帶qunit)。測試的東西,不使用需要功能或出口對象是好的,測試以上模塊如下:

/// <reference path="..\Resources\Scripts\LumaJS\stateMachine.js" /> 

test("starts with no state", function() { 
    var stateMachine = new StateMachine(); 

    equals(stateMachine.getCurrentStateLabel(), null); 
}); 

膽大妄爲失敗,出現「找不到變量」錯誤。

require = function() { } 
exports = {}; 

但後來我需要手動引用從我的測試模塊要求:

我已通過添加在fakeJSCommon.js下面,我再從測試文件中引用解決此得到這似乎並不理想。

所以我的問題是:有沒有辦法讓Chutzpah治療需要像引用標籤,或更好的方法來做到這一點,將實現我所期待的?

回答

0

Chutzpah只是在瀏覽器中運行你的代碼,所以你引用的任何東西都必須是可以加載的有效javascript。如果你使用的方法是「魔術」,並且沒有真正的定義,那麼Chutzpah就會失敗。我認爲你最好的選擇是有一個墊片(就像你所做的),防止Chutzpah不要在聲明中出錯。然後添加一個chutzpah.json文件來聲明您的參考。

+0

感謝馬修,我會通過.json試一試 – FlintZA

+0

謝謝,這完美地整理了它。我需要首先添加對我的fakeJSCommon的引用,以確保我所包含的文件在需求中也沒有失敗。這可能會允許我單元測試一些依賴於引擎細節(輸出爲TypeScript)的文件:) – FlintZA

相關問題