我有一个问题,包括在durandal环境中进行摩卡测试。我想从命令行使用mocha-phantomjs运行我的测试。该测试在浏览器中完美运行,但只要我尝试使用mocha-phantomjs,我就会收到以下错误消息(命令:mocha-phantomjs dummyPage.htm):
Error: Your custom reporter tried to require 'fs', but Mocha is not running in N
ode.js in mocha-phantomjs, so Node modules cannot be required - only other repor
ters
我的dummmy 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!')
});
});
})
答案 0 :(得分:0)
我没有使用过mocha-phantomjs,但您可能会遇到http://durandaljs.com/documentation/Native-Apps-With-Node-Webkit.html
中描述的问题接下来,因为节点有自己的require实现 与Durandal使用的require.js不同,我们需要修补 事情有点儿。为此,请将以下脚本块添加到 index.html的负责人:
<script type="text/javascript">
// this doesn't apply when not running with node webkit (nw)
// window.gui = require('nw.gui');
window.requireNode = window.require;
delete window.require;
window.requireNode.version = process.versions.node;
delete process.versions.node;
</script>