我有一个问题,包括在 durandal 环境中进行 mocha 测试。我想从命令行使用 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
我的虚拟 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!')
});
});
})