首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用mocha-phantomjs测试Node js应用程序

如何使用mocha-phantomjs测试Node js应用程序
EN

Stack Overflow用户
提问于 2013-05-14 14:18:39
回答 2查看 1.9K关注 0票数 1

我需要用mocha-phantomjs.I测试我的Node应用程序。我已经尝试了下面的代码来测试应用程序,但我得到了错误'ReferenceError: Can't find variable: require‘。如何解决这个问题。

test.html

代码语言:javascript
复制
<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

代码语言:javascript
复制
 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文件作为mocha-phantomjs test/test.html运行时,我得到的错误是

代码语言:javascript
复制
      ReferenceError: Can't find variable: require
EN

回答 2

Stack Overflow用户

发布于 2013-06-20 03:09:26

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

代码语言:javascript
复制
    <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>

因此,您将不需要要求src的任何内容。再说一次,就像你在客户端做所有的事情一样。

票数 2
EN

Stack Overflow用户

发布于 2013-05-15 05:43:29

看一下browserify,它使您能够自动包含npm库:https://github.com/substack/node-browserify

还推荐使用connect-browserify和asset-rack https://github.com/techpines/asset-rack,前者用于在开发https://github.com/andreypopp/connect-browserify中自动重新加载,后者用于在生产中自动绑定。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16536357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档