如何配置Rhino来运行AngularJS控制器的Jasmine测试

3
我遇到了一个问题,就是在使用Jasmine sbt插件测试Angular JS应用时无法正常运行单元测试。

当我将angular.js(版本1.3.1)添加到test.dependecies.js中时。

EnvJasmine.loadGlobal(EnvJasmine.libDir + "/angular.js");
EnvJasmine.loadGlobal(EnvJasmine.libDir + "/ui-bootstrap-0.11.2.js");
EnvJasmine.loadGlobal(EnvJasmine.testDir + "/lib/angular-mocks.js");

我遇到了以下错误

[Envjs/1.6(Rhino;U;Linux amd64 3.13.0-32-generic; en-US; rv:1.7.0.rc2) Resig/20070309 PilotFish/1.2.13 ] 无法读取文件:/opt/scala/myproject/src/main/webapp/static/js/lib/angular.js 错误为:TypeError:在[object HTMLDocument]对象中找不到函数querySelector。

我无法确定是 Angular 和 Rhino 之间存在兼容性问题,还是在 Jasmine 配置中出了问题。


1
运行现代应用程序(angular/backbone)测试服务器端使用rhino和/或htmlunit是我曾经尝试过的事情。但是,使用node(和真正的JS引擎)启动测试,在我看来是最简单和最快的方法。Rhino与最新规范不符,这可能是一条非常漫长的路,最终可能会遇到障碍。 - Pierre Gayvallet
你好。你用Rhino解决了这个问题吗?我遇到了相同的问题,正在思考是值得花力气去解决呢还是更好选择Karma。 - alekseevi15
我相信对于OP来说现在已经太晚了,但是现在有一个问题开放在sbt-jasmine-plugin上。我怀疑问题不在于rhino,而是在于env.js。正如@PierreGayvallet所指出的那样,rhino/env.js组合可悲地成为了消失在地平线上的死亡项目。 - joescii
1个回答

1

使用Domino等维护的项目:

Domino基于Mozilla的Andreas Gal的工作,目前似乎得到了维护。虽然它不能直接使用,但很容易修补。我所做的修改如下:

    更改文件以使用AMD样式加载而不是CommonJS(这样我可以使用RequireJS在Rhino和浏览器中加载它们)
    在序列化时向SVG元素添加xmlns属性
    修改CSS解析器以解决Rhino中大小写不敏感正则表达式的问题
    添加具有类似HTML元素的样式属性的SVG元素类型
    将SVG特定的CSS属性添加到CSS解析器中
    几个错误修复
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.tools.shell.Global;

import com.google.common.base.Charsets;

public abstract class RunJS {
    static final Log LOG = LogFactory.getLog(RunJS.class);

    static final int JS_VERSION = Context.VERSION_1_8;
    static final int OPTIMIZATION_LEVEL = 9;

    private static ScriptableObject ENVIRONMENT = null;

    static {
        ENVIRONMENT = createNewEnvironment();
    }

    /**
     * Creates a top level scope with the shell globals and requirejs then loads
     * bootstrap.js
     * 
     * The bootstrap script can then load other modules to be included in the top level
     * scope using requirejs
     */
    public static ScriptableObject createNewEnvironment() {
        Global global = null;

        Context cx = Context.enter();
        try {
            cx.setOptimizationLevel(OPTIMIZATION_LEVEL);
            cx.setLanguageVersion(JS_VERSION);

            global = new Global();
            global.setSealedStdLib(true);
            global.init(cx);

            Scriptable argsObj = cx.newArray(global, new Object[] {});
            global.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM);

            // Enable RequireJS
            loadJS(cx, global, "/path/to/r.js");

            // Load the bootstrap file
            loadJS(cx, global, "/path/to/bootstrap.js");

            global.sealObject();
        } catch (Exception e) {
            LOG.error("Error setting up Javascript environment", e);
        }
        finally {
            Context.exit();
        }
        return global;
    }

    public static void loadJS(Context cx, Scriptable scr, String fileName) throws Exception {
        Reader reader;
        try {
            reader = new InputStreamReader(new FileInputStream(new File(fileName)), Charsets.UTF_8);
        } catch (FileNotFoundException e) {
            throw new Exception("Could not find file", e);
        };

        try {
            cx.evaluateReader(scr, reader, fileName, 0, null);
        } catch (IOException e) {
            throw new Exception("IO error reading file", e);
        }
        finally {
            try { reader.close(); } catch (IOException e) {
                throw new Exception("IO error closing file", e);
            }
        }
    }

    protected static Scriptable getScope(Context cx) {
        Scriptable scope = cx.newObject(RunJS.ENVIRONMENT);
        scope.setPrototype(RunJS.ENVIRONMENT);
        scope.setParentScope(null);
        return scope;
    }
}

boostrap.js

require.config({
    baseUrl: '/base/path/to/packages'
});

var window, document, d3;

require(['domino/index'], function(domino) {
    window = domino.createWindow('');
    document = window.document;

    require(['d3/d3'], function(_d3) {
        d3 = _d3;
    });
});

// preload your modules
require(["mypackage/mymodule1", "mypackage/mymodule2"]);

Subclass RunJS and load your JavaScript file like this:

Context cx = Context.enter();
try {
    cx.setOptimizationLevel(OPTIMIZATION_LEVEL);
    cx.setLanguageVersion(JS_VERSION);

    Scriptable scope = getScope(cx);
    scope.put("myvar", scope, myvar);
    loadJS(cx, scope, "/path/to/yourcode.js");
}
catch (Exception e) {
    LOG.error(e);
}
finally {
    Context.exit();
}

参考资料


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接