我一直无法为NodeJS + Backbone应用程序设置测试框架,并且常量为“require not not defined”错误。我终于使用浏览器中的测试框架来工作,该框架获取了我需要的所有依赖项并运行了test.js文件。
目前,我只对基础模型,视图和集合进行基本测试。现在,我想添加API测试,但我又回到了“require not not defined”错误。是什么造成的?很明显,我在这里缺少一些基本的东西。我只想补充一下:
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("");
}
})
});
});
测试runner.html:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Title & 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>
答案 0 :(得分:3)
require
和commonjs仅适用于Node.js
如果您运行浏览器测试,则需要对其进行编码,就像您将在浏览器中运行它一样。另请注意,单元测试应该单独完成,您不需要加载app server(express)来运行测试。
我想从那里向您指出一个简单的解决方案,但是有太多的选择。基本上,你应该通过加载一个html文件开始在浏览器中运行浏览器测试。
然后,您需要自动执行此操作并从终端运行浏览器测试。那时你想在PhantomJs中运行测试,并在终端上输出浏览器结果。在此之前,您可以查看Karma和Testem谁是两个浏览器测试运行器(请记住,此处Mocha不会通过命令行运行浏览器测试)。
当您使用Backbone时,您可能会对Backbone-Boilerplate Karma + Grunt测试设置感兴趣作为起点。在此处详细了解:https://github.com/backbone-boilerplate/backbone-boilerplate