Requirejs 在Durandal环境中使用mochaphantom js运行Mocha测试时出错
我有一个问题,包括在杜兰达尔环境中的摩卡测试。我想从命令行用mochaphantomjs运行测试。该测试在浏览器中运行良好,但当我尝试使用mocha phantomjs时,我会收到以下错误消息(命令:mocha phantomjs dummyPage.htm):Requirejs 在Durandal环境中使用mochaphantom js运行Mocha测试时出错,requirejs,mocha.js,durandal,mocha-phantomjs,Requirejs,Mocha.js,Durandal,Mocha Phantomjs,我有一个问题,包括在杜兰达尔环境中的摩卡测试。我想从命令行用mochaphantomjs运行测试。该测试在浏览器中运行良好,但当我尝试使用mocha phantomjs时,我会收到以下错误消息(命令:mocha phantomjs dummyPage.htm): <script type="text/javascript"> require.config({ baseUrl: '../app/', paths: { 'a
<script type="text/javascript">
require.config({
baseUrl: '../app/',
paths: {
'app': '../app',
'specs': '../sampleTest/specs/',
'text': '../lib/require/text',
'durandal': '../lib/durandal/js',
'plugins' : '../lib/durandal/js/plugins',
'transitions' : '/lib/durandal/js/transitions',
'knockout': '../lib/knockout/knockout-2.3.0',
'jquery': '../lib/jquery/jquery-1.9.1'
}
});
var runTests = function (specfiles) {
// Initialize mocha and leak assert into the global space.
mocha.setup('bdd');
mocha.reporter('html');
assert = chai.assert;
require(specfiles, function () {
require(['specs/test.spec'],function(spec){
if (window.mochaPhantomJS) {
console.log('test with phantomJS')
mochaPhantomJS.run();
}
else {
mocha.run();
}
});
});
};
runTests();
</script>
我的Dummy html页面如下所示:
<script type="text/javascript">
require.config({
baseUrl: '../app/',
paths: {
'app': '../app',
'specs': '../sampleTest/specs/',
'text': '../lib/require/text',
'durandal': '../lib/durandal/js',
'plugins' : '../lib/durandal/js/plugins',
'transitions' : '/lib/durandal/js/transitions',
'knockout': '../lib/knockout/knockout-2.3.0',
'jquery': '../lib/jquery/jquery-1.9.1'
}
});
var runTests = function (specfiles) {
// Initialize mocha and leak assert into the global space.
mocha.setup('bdd');
mocha.reporter('html');
assert = chai.assert;
require(specfiles, function () {
require(['specs/test.spec'],function(spec){
if (window.mochaPhantomJS) {
console.log('test with phantomJS')
mochaPhantomJS.run();
}
else {
mocha.run();
}
});
});
};
runTests();
</script>
define(['viewmodels/flickr'], function (flickr) {
describe('Flickr-Test', function(){
it('displayName should be equal to Flickr', function () {
assert.equal(flickr.displayName,'Flickr','should load right view model');
});
it('state should be pending', function () {
assert.equal(flickr.activate().state(),'pending','state should be pending');
});
});
describe("DOM Test", function () {
var el = document.createElement("div");
el.id = "myDiv";
el.innerHTML = "Hello World!";
document.body.appendChild(el);
var myEl = document.getElementById('myDiv');
it("has the right text", function () {
assert.equal(myEl.innerHTML,'Hello World!')
});
});
})
<script type="text/javascript">
require.config({
baseUrl: '../app/',
paths: {
'app': '../app',
'specs': '../sampleTest/specs/',
'text': '../lib/require/text',
'durandal': '../lib/durandal/js',
'plugins' : '../lib/durandal/js/plugins',
'transitions' : '/lib/durandal/js/transitions',
'knockout': '../lib/knockout/knockout-2.3.0',
'jquery': '../lib/jquery/jquery-1.9.1'
}
});
var runTests = function (specfiles) {
// Initialize mocha and leak assert into the global space.
mocha.setup('bdd');
mocha.reporter('html');
assert = chai.assert;
require(specfiles, function () {
require(['specs/test.spec'],function(spec){
if (window.mochaPhantomJS) {
console.log('test with phantomJS')
mochaPhantomJS.run();
}
else {
mocha.run();
}
});
});
};
runTests();
</script>
我没有使用过mocha phantomjs,但您可能会遇到中描述的问题
<script type="text/javascript">
require.config({
baseUrl: '../app/',
paths: {
'app': '../app',
'specs': '../sampleTest/specs/',
'text': '../lib/require/text',
'durandal': '../lib/durandal/js',
'plugins' : '../lib/durandal/js/plugins',
'transitions' : '/lib/durandal/js/transitions',
'knockout': '../lib/knockout/knockout-2.3.0',
'jquery': '../lib/jquery/jquery-1.9.1'
}
});
var runTests = function (specfiles) {
// Initialize mocha and leak assert into the global space.
mocha.setup('bdd');
mocha.reporter('html');
assert = chai.assert;
require(specfiles, function () {
require(['specs/test.spec'],function(spec){
if (window.mochaPhantomJS) {
console.log('test with phantomJS')
mochaPhantomJS.run();
}
else {
mocha.run();
}
});
});
};
runTests();
</script>
接下来,因为节点有它自己的require实现
与Durandal使用的require.js不同,我们需要修补
事情有点复杂了。为此,将以下脚本块添加到
index.html的标题:
<script type="text/javascript">
require.config({
baseUrl: '../app/',
paths: {
'app': '../app',
'specs': '../sampleTest/specs/',
'text': '../lib/require/text',
'durandal': '../lib/durandal/js',
'plugins' : '../lib/durandal/js/plugins',
'transitions' : '/lib/durandal/js/transitions',
'knockout': '../lib/knockout/knockout-2.3.0',
'jquery': '../lib/jquery/jquery-1.9.1'
}
});
var runTests = function (specfiles) {
// Initialize mocha and leak assert into the global space.
mocha.setup('bdd');
mocha.reporter('html');
assert = chai.assert;
require(specfiles, function () {
require(['specs/test.spec'],function(spec){
if (window.mochaPhantomJS) {
console.log('test with phantomJS')
mochaPhantomJS.run();
}
else {
mocha.run();
}
});
});
};
runTests();
</script>
//当不使用节点webkit(nw)运行时,这不适用
//window.gui=require('nw.gui');
window.requireNode=window.require;
删除window.require;
window.requireNode.version=process.versions.node;
删除process.versions.node;
当我将该部分放入标题时,我得到以下错误:错误:尚未为上下文加载模块名“nw.gui”:。上面的Use require([])代码是节点webkit的引用解决方案。我已经更新了代码示例。