本文介绍了如何配置Rhino为angularjs控制器运行茉莉花测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Jasmine sbt插件使Angular JS应用程序的单元测试无法正常工作.

I am having trouble getting unit tests working for an Angular JS app using Jasmine sbt plugin.

当我将angular.js(1.3.1版)添加到test.dependecies.js

when I add angular.js ( ver 1.3.1) to 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");

我遇到了以下错误

我无法确定角度和犀牛或茉莉花配置是否存在兼容性问题

I cant figure if there is a compatibility issue with angular and rhino or in jasmine config

推荐答案

使用维护的项目,例如Domino:

Use a maintained project such as Domino:


    Change files to use AMD style loading instead of CommonJS (so I can load them in Rhino and the browser using RequireJS)
    Add xmlns attribute to SVG element when serializing
    Modified the CSS parser to workaround a issue in Rhino with case insensitive regular expressions
    Added a SVG element type which has a style property like the HTML elements
    Added SVG specific CSS properties to the CSS parser
    Several bug fixes
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();
}

参考

Domino(已提供SVG支持)

这篇关于如何配置Rhino为angularjs控制器运行茉莉花测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 19:23
查看更多