ember.js - Mocha 测试在 yeoman ember 生成器中不起作用

标签 ember.js mocha.js yeoman

我几乎没有修改 ember 生成器生成的代码

哟 Ember

并稍微更改 test/spec/test.js

'use strict';
(function () {
    describe('Give it some context', function () {
        describe('maybe a bit more context here', function () {
            it('should equal to the title', function () {
                var title = document.title;
                title.should.equal('Ember Starter Kit');
                            console.log(title) //doesn't output anything in the terminal
            });
            it('should equal to a number', function () {
                1.should.equal(1);
            });
        });
    });
})();

有两件奇怪的事情:

  1. console.log 不会在终端中输出任何内容
  2. 显示 0 次测试:

    运行“mocha:all”( Mocha )任务 测试:http://..*.*/index.html

    0 次测试完成(1 毫秒)

    完成,没有错误。

最佳答案

我注意到,当您收到“0 个测试完成”时,测试文件中存在错误。

您的测试有两个问题:

  1. console.log(title) 后缺少分号
  2. 对 Integer 执行方法是语法错误。请尝试以下操作:

期望(1).to.等于(1);

此外,请务必在包含 Chai 后的 index.html 中包含以下几行(以便应该 正常工作):

...
<!-- assertion framework -->
<script src="lib/chai.js"></script>
<script>var expect = chai.expect</script>
<script>var should = chai.should()</script>
...

这会导致您的测试运行。但是,我没有控制台日志记录问题的答案。我自己也在寻找答案。似乎是 grunt-mocha 和 PhantomJS 的东西。

编辑:事实证明,grunt-mocha 中的控制台日志记录被禁用: grunt-mocha source

如果您进入项目目录中的 node_modules/grunt-mocha/tasks/mocha.js,您可以取消注释第 101 行,日志记录将适用于您的项目。

完整更新的测试文件:

/*global describe, it, document, expect */
'use strict';
(function () {
    describe('Give it some context', function () {
        describe('maybe a bit more context here', function () {
            it('should equal to the title', function () {
                var title = document.title;
                title.should.equal('Ember Starter Kit');
                            console.log(title); //doesn't output anything in the terminal
            });
            it('should equal to a number', function () {
                expect(1).to.equal(1);
            });
        });
    });
})();

编辑:来自 grunt-mocha 的开发者:

是的,取消注释该行以在命令行中获取控制台输出...尽管通常在运行自动化测试时您不希望这样做,因此默认情况下它被禁用。如果您想真正进行调试,我建议在浏览器中打开规范并使用浏览器控制台/调试器。额外的好处是您可以单击套件/测试,以便它只运行您感兴趣的测试。

关于ember.js - Mocha 测试在 yeoman ember 生成器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073949/

相关文章:

javascript - API 失败时 Ember.js 模型的默认值

node.js - Heroku 部署错误 : Cannot find module './errors/cast'

yeoman - 添加什么 &lt;style type ="text/css">body { display : none ! important }&lt;/style&gt;

javascript - 运行 Grunt 时将依赖项(如 jQuery)注入(inject)库

ember.js - Ember 表与 Ember 模型/Ember 数据集成

ember.js - Ember Simple Auth - 将当前用户注入(inject)每条路由

Ember.js:折叠/延迟昂贵的观察者或计算属性

javascript - 如何从 selenium javascript 中返回的 findElement 中查找元素

javascript - 如何在 Sinon.js 中测试 Controller 中使用的辅助函数

typescript - VSCode : Tests won't proceed after using executeCommand to open a sample project