2

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

yo 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 毫秒)

    完成,没有错误。

4

1 回答 1

2

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

你的测试有两个问题:

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

expect(1).to.equal(1);

此外,请务必在index.htmlChai 包含之后包含以下行(这样才should有效):

...
<!-- 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 的开发者:

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

于 2013-04-20T01:26:15.503 回答