0
私はノードでWeb開発& Expressの第5章(品質保証)を使用しています。私はMochaとChaiを設定しましたが、URLに?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>
パブリック/ QA /テスト-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
私は間違っていますが、どうすればモカのテストが表示されるのですか?追加のファイルが必要な場合は、尋ねてください。
これは大きな助けになりましたが、それでも機能しません。私は今どうすればいい? – CDuck
@CDuckわからない。私は私のマシンでそれをテストし、それは私のために働いています。クエリ文字列、つまり 'http:// localhost:3000 /?test = 1'を入力しましたか?そして、サーバー側の端末やクライアント側のコンソールのいずれかでエラーが発生していますか? – McMath
これで3つの変更を加えれば、私にはうってつけの情報が必要になります。あなたのディレクトリ構造はどのように見えますか? – McMath