[英]Mocha Tests Not Showing Up
I am working through Web Development with Node & Express , Chapter 5 (Quality Assurance). 我正在使用Node&Express的第5章(质量保证)进行Web开发 。 I have set up Mocha and Chai, but the tests are not showing up, even when I add the ?tests=1 onto the url.
我已经设置了Mocha和Chai,但是即使将?tests = 1添加到URL上,测试也不会显示。
meadowlark.js (main entry point): 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 (main template): 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 (location of Mocha tests): public / qa / tests-global.js(Mocha测试的位置):
suite('Global Tests', function() {
test('page has a valid title', function() {
assert(document.title && document.title.match(/\S/) &&
document.title.toUpperCase() !=== 'TODO');
});
});
Folder Structure: 资料夹结构:
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
What am I doing wrong, and how can I make it so the Mocha tests show up? 我在做什么错,我该如何做才能使Mocha测试显示出来? If you need any additional files, ask.
如果需要其他文件,请询问。
You have two spelling mistakes and one syntax error. 您有两个拼写错误和一个语法错误。
In meadowlark.js
, change req.qurey.text
to req.query.test
: 在
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();
});
In main.handlebars
, change {{#if showTexts}}
to {{#if showTests}}
: 在
main.handlebars
,将{{#if showTexts}}
更改为{{#if showTests}}
:
{{#if showTests}}
<link rel="stylesheet" href="/vendor/mocha.css">
{{/if}}
And in public/qa/tests-global.js
, the not-equals operator should be !==
rather than !===
: 在
public/qa/tests-global.js
,不等于运算符应为!==
而不是!===
:
test('page has a valid title', function() {
assert(document.title && document.title.match(/\S/) &&
document.title.toUpperCase() !== 'TODO');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.