如何确保在节点上使用mocha和phantomjs进行每个testing的未触及的HTML

我写了一个web应用程序,我想要使用摩卡和phantomjs从命令行做一些DOMtesting,我想找出最好的方法来做到这一点。

由于networking应用程序正在进行大量的DOM操作,我希望能够在每次testing中加载全新的DOM。 我试图使用phantomjs的网页模块,但我不知道如何在页面上运行摩卡testing(或者如果这是正确的方式)。

当然,我可以创build一个文件test.html ,其中包含通过脚本标签的mocha,然后运行phantomjs test.html 。 但是我怎样才能确保在每个testing中都没有触动DOM? 为每个具有相同内容的testing添加一个HTML文件将是一个噩梦来支持。

我到目前为止:

我的testrunner.js:

 var Mocha = require('mocha'), expect = require('expect.js'), fs = require('fs'), path = require('path'); var mocha = new Mocha( { ui: 'bdd' }); // I only add one file for now mocha.addFile( path.join(__dirname, 'tests_integration/test.js') ); mocha.run(function(failures){ process.on('exit', function () { process.exit(failures); }); }); 

我的test.js:这是我奋斗的地方,因为我不知道如何在页面内运行testing:

 var phantom = require('phantom'); phantom.create(function (ph) { ph.createPage(function (page) { page.open("http://test.local", function (status) { page.evaluate(function (tests) { // here I need to run my tests, which obviously doesnt work that way describe('DOM tests',function(){ it('should be able to acces the page',function(){ expect(document.body).not.to.be(undefined) }); }); return document.body }.bind(tests), function (html) { ph.exit(); }); }); }); }); 

要运行testing,我执行node integration-runner.js 。 我想到的一个解决scheme就是使用includeJS将mocha注入幻像页面,但对我来说这似乎有点不顺手。

我完全错了吗?

好吧,经过一些研究和其他开发人员的交谈,似乎创build不同的HTML文件将是最干净的解决scheme。 但是,因为我不想手动更新所有这些文件,所以当我更改我的webapp时,我决定使用injectJS解决scheme,如在这个伟大的职位描述 。

现在我可以dynamic地将摩卡testing文件注入到现场并运行它们。