简体   繁体   English

使用Chutzpah测试duktape中使用的CommonJS模块

[英]Using Chutzpah to test CommonJS modules used in duktape

I'm working in a game engine ( Atomic Game Engine ) which uses the Duktape embeddable javascript engine. 我正在使用Duktape可嵌入javascript引擎的游戏引擎( Atomic Game Engine )中工作。 This allows loading of CommonJS modules, so I have modules like the following: 这允许加载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);
}

I've got Chutzpah set up to unit test in VS Community Edition (with qunit). 我已经将Chutzpah设置为在VS Community Edition(带有qunit)中进行单元测试。 Testing something that doesn't use the require function or export object is fine, testing the above module as follows: 测试不使用require函数或导出对象的东西很好,可以如下测试上述模块:

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

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

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

Chutzpa fails with a "Can't find variable" error. Chutzpa失败,并显示“找不到变量”错误。

I've got around this by adding the following in a fakeJSCommon.js which I then reference from test files: 我通过在fakeJSCommon.js中添加以下内容来解决此问题,然后从测试文件中进行引用:

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

But I then need to manually reference the requires from the modules I'm testing as well, which doesn't seem ideal. 但是我还需要从正在测试的模块中手动引用需求,这似乎并不理想。

So my question is: Is there a way to get Chutzpah to treat requires like reference tags, or a better way to do this that would achieve what I'm looking for? 所以我的问题是: 是否有一种方法可以使Chutzpah像参考标记那样对待要求,或者有一种更好的方法可以实现我所寻找的目标?

Chutzpah just runs your code in the browser so anything you reference needs to be valid javascript that it can load. Chutzpah只是在浏览器中运行您的代码,因此您引用的任何内容都必须是可以加载的有效JavaScript。 If you are using methods that are "magic" and have no real definition than Chutzpah will fail on them. 如果您使用的是“魔术”方法,并且没有真正的定义,那么Chutzpah会对它们失败。 I think you best option is to have a shim (like you made) which prevents Chutzpah to not error on the statement. 我认为您最好的选择是使用垫片(如您制作的垫片),以防止Chutzpah在声明中出现错误。 And then add a chutzpah.json file to declare your references. 然后添加一个chutzpah.json文件来声明您的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM