用摩卡testingNodeJS – “需求未定义”

我一直在设置NodeJS + Backbone应用程序的testing框架时遇到了常量“require is not defined”错误。 我终于得到它使用浏览器内testing框架工作,拿起我需要的所有依赖项,并运行一个test.js文件。

目前,我只对我的Backbone模型,视图和集合进行基本testing。 现在,我想添加APItesting,但我回到相同的“要求没有定义”的错误。 这是什么原因造成的? 很显然,我在这里错过了一些基本的东西。 我只想补充一下:

var request = require('supertest') , express = require('express'); var app = express(); 

test.js的片段:

 describe('Application', function(){ it("creates a global variable for the namespace", function() { should.exist(App); }) }); describe('Models', function() { describe('SearchFormModel', function() { beforeEach(function() { this.SearchFormModel = new App.Model.SearchFormModel(); this.defaultFields = this.SearchFormModel.attributes; }) it("created a SearchFormModel", function() { should.exist(this.SearchFormModel); }) it("should have 7 default fields", function() { Object.keys(this.SearchFormModel).length.should.equal(7); }) it("should default all fields to empty string", function() { for (var key in this.defaultFields) { this.defaultFields[key].should.equal(""); } }) }); }); 

testingrunner.html:

 <!DOCTYPE html> <html lang="en"> <head> <!-- Title &amp; Meta --> <title>Frontend tests</title> <meta charset="utf-8"> <!-- Stylesheets --> <link rel="stylesheet" href="../node_modules/mocha/mocha.css"> </head> <body> <div id="mocha"></div> <!-- Testing Libraries --> <script src="../node_modules/mocha/mocha.js"></script> <script src="../node_modules/chai/chai.js"></script> <script> // Use the expect version of chai assertions - http://chaijs.com/api/bdd var should = chai.should(); // Tell mocha we want TDD syntax mocha.setup('tdd'); </script> <!-- Libs --> <script src="../public/lib/jquery-1.8.2.min.js"></script> <script src="../public/lib/underscore-min.js"></script> <script src="../public/lib/backbone-min.js"></script> <script src="../public/lib/bootstrap.min.js"></script> <script src="../public/lib/highcharts.js"></script> <script src="../public/lib/bootstrap-datepicker.js"></script> <script src="../public/js/modules/exporting.js"></script> <!-- Source files --> <script src="../public/js/namespace.js"></script> <script src="../public/js/jst.js"></script> <script src="../public/js/utils.js"></script> <script src="../public/js/models/models.js"></script> <script src="../public/js/models/search.js"></script> <script src="../public/js/models/plot.js"></script> <script src="../public/js/models/search_result.js"></script> <script src="../public/js/views/header.js"></script> <script src="../public/js/views/plot.js"></script> <script src="../public/js/views/list.js"></script> <script src="../public/js/views/search.js"></script> <script src="../public/js/router.js"></script> <script src="../public/js/app.js"></script> <!-- Test --> <script src="test.js"></script> <script> mocha.run(); </script> </body> </html> 

require和commonjs只能在Node.js中使用

如果你运行浏览器testing,那么你需要编码,就像你会在浏览器中运行它。 另外请注意,unit testing应该是孤立的,你不应该加载你的应用程序服务器(快递)来运行你的testing。

我想指出一个简单的解决scheme,但是有太多的select。 基本上,您应该通过加载一个html文件来开始在浏览器中运行浏览器testing。

然后,你会想自动化这个并从terminal运行浏览器testing。 这就是当你想在PhantomJs中运行testing,并在terminal上输出浏览器结果的时候。 在这附近,你可以检查Karma和Testem谁是两个浏览器testing亚军(记住这里摩卡独自不会通过命令行运行浏览器testing)。

在使用Backbone的时候,你可能会对Backbone-Boilerplate Karma + Grunttesting设置感兴趣。 在这里看到更多: https : //github.com/backbone-boilerplate/backbone-boilerplate