【问题标题】:Why are mocha test not running? - node.js为什么摩卡测试没有运行? - 节点.js
【发布时间】: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


【解决方案1】:

自从您发布此内容以来已经有一段时间了。 我现在正在看这本书,我也遇到了同样的问题。 我意识到这是因为我打开了:

localhost:3000/?test=1 

代替:

localhost:3000/about?test=1

【讨论】:

    【解决方案2】:

    书上的例子是正确的。这可能是文件或文件结构的命名约定问题。我有类似的问题,但我的文件名称错误。例如“测试”与“测试”。

    【讨论】:

      猜你喜欢
      • 2021-02-15
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2017-07-25
      相关资源
      最近更新 更多