我正在使用Node&amp ;;进行 Web开发。快递,第5章(质量保证)。我已经设置了Mocha和Chai,但测试没有出现,即使我将?tests = 1添加到网址上。
meadowlark.js(主要入口点):
var express = require('express');
var app = express();
var handlebars = require('express-handlebars').create({ defaultLayout: 'main' });
// express handles normalizing url
app.set('port', process.env.PORT || 3000);
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.use(express.static(__dirname + '/public'));
app.use(function(req, res, next) {
res.locals.showTests = app.get('env') !== 'production' && req.query.text === '1';
next();
});
app.get('/', function(req, res) {
res.render('home');
});
app.get('/about', function(req, res) {
res.render('about');
});
// 404 catch-all handler (middleware)
app.use(function(req, res) {
res.status(404);
res.render('404');
});
// 500 error handler (middleware)
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500);
res.render('500');
});
app.listen(app.get('port'), function() {
console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.');
});
main.handlebars(主模板):
<!DOCTYPE html>
<html>
<head>
<title>Meadowlark Travel</title>
{{#if showTexts}}
<link rel="stylesheet" href="/vendor/mocha.css">
{{/if}}
<script src="http://code.jquery.com/jquery-3.0.0.js" integrity="sha256-jrPLZ+8vDxt2FnE1zvZXCkCcebI/C8Dt5xyaQBjxQIo=" crossorigin="anonymous"></script>
</head>
<body>
<header><img src="/img/logo.png" alt="Meadowlark Travel logo"></header>
{{{body}}}
{{#if showTests}}
<div id="mocha"></div>
<script src="/vendor/mocha.js"></script>
<script src="/vendor/chai.js"></script>
<script>
mocha.ui('tdd');
var assert = chai.assert;
</script>
<script src="/qa/tests-global.js"></script>
{{#if pageTestScript}}
<script src="{{pageTestScript}}"></script>
{{/if}}
<script>mocha.run();</script>
{{/if}}
</body>
</html>
public / qa / tests-global.js(摩卡测试的位置):
suite('Global Tests', function() {
test('page has a valid title', function() {
assert(document.title && document.title.match(/\S/) &&
document.title.toUpperCase() !=== 'TODO');
});
});
文件夹结构:
meadowlark.js
node_modules > *node.js/npm*
package.json
public > qa > tests-global.js
> vendor > chai.js,
> mocha.css,
> mocha.js
views > home.handlebars
> layouts > main.handlebars
我做错了什么,我怎样才能让Mocha测试显示出来?如果您需要任何其他文件,请询问。
答案 0 :(得分:2)
您有两个拼写错误和一个语法错误。
在meadowlark.js
中,将req.qurey.text
更改为req.query.test
:
app.use(function(req, res, next) {
res.locals.showTests = app.get('env') !== 'production' && req.query.test === '1';
next();
});
在main.handlebars
中,将{{#if showTexts}}
更改为{{#if showTests}}
:
{{#if showTests}}
<link rel="stylesheet" href="/vendor/mocha.css">
{{/if}}
在public/qa/tests-global.js
中,not-equals运算符应为!==
而不是!===
:
test('page has a valid title', function() {
assert(document.title && document.title.match(/\S/) &&
document.title.toUpperCase() !== 'TODO');
});