我在使用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。
我无法确定角度和犀牛或茉莉花配置是否存在兼容性问题
使用诸如Domino之类的维护项目:
Domino是基于Mozilla的Andreas Gal的工作,目前似乎正在维护。虽然开箱即用,但修补起来很容易。我所做的修改如下:
更改文件以使用AMD样式加载而不是CommonJS(因此我可以使用RequireJS在Rhino和浏览器中加载它们) 序列化时将xmlns属性添加到SVG元素 修改了CSS解析器,以解决不区分大小写的正则表达式在Rhino中的问题 添加了具有样式属性(如HTML元素)的SVG元素类型 向CSS解析器添加了SVG特定的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();
}
参考
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句