我正在开发一个应用程序,我想在我的数据库上编写一些测试。
当我想运行我的测试代码时,它在我的 dbtest.js 文件中给出了以下错误:
ReferenceError: Can't find variable: require
更新
我不知道我必须使用require.js
,但现在我已经下载了它,但我不知道如何配置它。我需要做些什么改变才能实现这个目标?
以下是我的文件:
dbtest.js
var Database = require(['./../server/database.js'])
describe('Database', function() {
var db = new Database();
...
...
的index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Mocha Spec Runner</title>
<link rel="stylesheet" href="../vendor/mocha.css">
</head>
<body>
<div id="mocha"></div>
<script src="../vendor/mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script src="../vendor/chai.js"></script>
<script>
var assert = chai.assert;
var expect = chai.expect;
var should = chai.should();
</script>
<script src="../vendor/require.js"></script>
<!-- include source files here... -->
<!-- include spec files here... -->
<script src="../dbtest.js"></script>
<script>
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
} else {
mocha.run();
}
</script>
</body>
</html>
testapp.js(快速应用):
var express = require('express');
var path = require('path');
var http = require('http');
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('jade').renderFile);
app.use(express.static(path.join(__dirname, 'views')));
app.get('/', function (req, res) {
res.render('index.html');
});
// start up server
var PORT = 3001;
var server = http.createServer( app ).listen( PORT, function() {
console.log('Express server listening on port ' + PORT);
});
Gruntfile.js
// Express Config
express: {
options: {
// Override defaults here
},
dev: {
options: {
port: 3000,
script: 'app.js'
}
},
test: {
options: {
port: 3001,
script: 'test/testapp.js'
}
}
},
mocha_phantomjs: {
options: {
'reporter': 'spec'
},
all: ['test/views/**/*.html']
},
//Test
grunt.registerTask('test', 'Run the tests.', [
'clean:server',
'jshint:test',
'express:test',
//'open:test',
'mocha_phantomjs'
]);