2013-05-14 92 views
1

我需要使用mocha-phantomjs.测试我的Node js应用程序。我尝试了下面的代码来测试应用程序,但我得到的错误是'ReferenceError:Can not找到变量:require'.How解决这个问题。如何使用mocha-phantomjs测试Node js应用程序

的test.html

<html> 
<head> 
    <title> Tests </title> 
    <link rel="stylesheet" href="./node_modules/mocha/mocha.css" /> 
</head> 
<body> 
    <div id="mocha"></div> 
    <script src="../node_modules/mocha/mocha.js"></script> 
    <script src="../node_modules/should/lib/should.js"></script> 

    <script> 
     mocha.ui('bdd'); 
     mocha.reporter('html'); 

     </script> 
    <script src="test.js"></script> 
    <script> 
     if (window.mochaPhantomJS) { mochaPhantomJS.run(); } 
     else { mocha.run(); } 
    </script> 
</body> 
</html> 

test.js

var module=require('../lib/requiredModule'); 
var should = require('chai').should(); 
describe('Testing',function(){ 

    it('Save Data',function(){ 
     module.save(content,function(err,res){ 
      should.not.exist(err); 
     }); 
    }); 
    }); 

当运行HTML文件作为摩卡phantomjs测试/ test.html的我得到错误的

 ReferenceError: Can't find variable: require 
+1

浏览器不需要它的API,phantomjs和mocha-phantomjs不会为你介绍它。 – 2013-05-14 14:03:51

回答

2

所以,我认为你的问题是通过测试运行器运行你的测试基本上运行它们,就好像它们是客户端。因此,它将无法找到您的本地节点模块(如require)。您可以尝试直接加载require.js。或者,只需使用

<script src="../node_modules/chai/chai.js"></script> 
    <script> 
     mocha.ui('bdd'); 
     mocha.reporter('html'); 
     var should = chai.should; // This will give you access to chai should. 
    </script> 

所以你不需要任何你需要的东西。再次想到,就像你在做一切客户端一样。