【发布时间】:2014-12-17 21:56:45
【问题描述】:
我正在使用来自 O'reilly 的 使用 node 和 express 进行 Web 开发。刚刚介绍了使用 Mocha 进行测试,我正在尝试运行 2 个测试。首先是一个全局测试来验证是否有页面标题,其次是一个页面特定测试来检查到联系页面的链接。 Mocha 在我测试页面时加载,但没有运行测试,因为我得到“passes:0 failed:0”。
我的主应用文件中有这段代码:
app.use(function(req,res,next){
res.locals.showTests = app.get('env') !== 'production' && req.query.test === '1';
next();
});
app.get('/', function(req, res){
res.render('home');
});
app. get('/about', function(req,res){
res.render('about', {fortune: fortune.getFortune(),
pageTestScript: '/qa/tests-about.js'
});
});
这是我的 tests-about.js
suite('"About" Page Tests', function(){
test('page should contain link to contact page', function(){
assert($('a[href="/contact"]').length);
});
});
这是我的测试-global.js
suite('Global Tests', function(){
test('page has a valid title', function(){
assert(document.title && document.title.match(/\S/) &&
document.title.toUpperCase() !=== 'TODO');
});
});
这是我的主要模板文件(车把)
<!doctype html>
<html>
<head>
<title>Random Pixel Media</title>
{{#if showTests}}
<link rel="stylesheet" href="/vendor/mocha">
{{/if}}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<header><a href="/"><img src="/img/logo.png" alt="Random Pixel Logo" /></a></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>
根据这本书,当我访问 about 页面时,我应该看到 2 个套件和 1 个失败,但正如我所说,我看到一个空白的项目符号,通过:0 和失败:0 任何帮助理解我所缺少的东西将不胜感激。
【问题讨论】:
-
你如何运行 mocha + 你的测试在哪里?如果您只是运行
mocha,它将运行匹配模式./test/*.js的测试 -
如果
pageTestScript未设置,您将获得您报告的结果。 (@topheman Mocha 可以在服务器端或浏览器端运行。OP 正在运行 Mocha 浏览器端。)
标签: javascript node.js testing mocha.js chai