Javascript 使用Chutzpah测试duktape中使用的CommonJS模块
我在一个使用可嵌入javascript引擎的游戏引擎()中工作。这允许加载模块,因此我有如下模块:Javascript 使用Chutzpah测试duktape中使用的CommonJS模块,javascript,visual-studio,unit-testing,commonjs,chutzpah,Javascript,Visual Studio,Unit Testing,Commonjs,Chutzpah,我在一个使用可嵌入javascript引擎的游戏引擎()中工作。这允许加载模块,因此我有如下模块: require("Scripts/LumaJS/Interface"); // Interface to be implemented by state objects var IState = new Interface({ optional: ["onEnter", "onExit", "onUpdate"], reserved: ["label"] }); StateMa
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)中设置了Chutzpah进行单元测试。测试不使用require函数或export对象的东西是可以的,测试上述模块如下:
/// <reference path="..\Resources\Scripts\LumaJS\stateMachine.js" />
test("starts with no state", function() {
var stateMachine = new StateMachine();
equals(stateMachine.getCurrentStateLabel(), null);
});
但是我还需要手动引用我正在测试的模块的需求,这似乎并不理想
所以我的问题是:有没有一种方法可以让Chutzpah处理引用标记之类的需求,或者有更好的方法来实现我想要的目标?Chutzpah只是在浏览器中运行代码,所以您引用的任何内容都需要是有效的javascript,它可以加载。如果您使用的方法是“神奇的”,并且没有真正的定义,那么Chutzpah将在这些方法上失败。我认为你最好的选择是有一个垫片(就像你做的那样),它可以防止胡言乱语在语句中出错。然后添加一个文件来声明您的引用。谢谢Matthew,我将通过.json a tryThanks提供导入,这将对其进行完美排序。我需要首先添加对我的fakeJSCommon的引用,以确保我包含的文件也不会在要求上失败。这可能允许我对一些依赖于引擎细节(输出为TypeScript)的文件进行单元测试:)
require = function () { }
exports = {};