【问题标题】:Mocha tests with Yeoman and PhantomJS使用 Yeoman 和 PhantomJS 进行 Mocha 测试
【发布时间】:2013-01-08 16:04:43
【问题描述】:

我正在使用Yeoman 来搭建我的项目。它附带了几个方便的东西,包括一个基于PhantomJS 的测试运行器。

我的问题是,虽然我的测试在浏览器中正确运行,但在 CLI 中尝试使用 PhantomJS 运行它们时会超时。

这是我的测试index.html 的样子:

<!doctype html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Mocha Spec Runner</title>
  <link rel="stylesheet" href="lib/mocha/mocha.css">
</head>
<body>
  <div id="mocha"></div>
  <script src="lib/mocha/mocha.js"></script>
  <!-- assertion framework -->
  <script src="lib/chai.js"></script>

  <!-- include source files here... -->
  <script data-main="scripts/main" src="scripts/vendor/require.js"></script>

  <script>
    mocha.setup({ui: 'bdd', ignoreLeaks: true});
    expect = chai.expect;
    should = chai.should();
    require(['../spec/map.spec'], function () {
      setTimeout(function () {
        require(['../runner/mocha']);
      }, 100);
    });
  </script> 

</body>
</html>

这里是map.spec.js

require(['map'], function (Map) {
  describe('Choropleth Map Generator', function() {
    describe('Configure the map', function () {
      it("should enforce mandatory parameters in the configuration", function () {
        var config = {title: 'test configuration'};
        var map = new Map(config);
        (function () {
          map.getConfig();
        }).should.throw(Error);
      });
    });
  });
});

现在,当我执行yeoman test 时,我得到了这个:

Running "server:phantom" (server) task

Starting static web server on port 3501

[...]

Running "mocha:all" (mocha) task
Testing index.html
<WARN> PhantomJS timed out, possibly due to a missing Mocha run() call. Use --force to continue. </WARN>

Aborted due to warnings.

正如我所说,yeoman server:test 在浏览器中正确显示了我的断言。

我正在使用 Yeoman 0.9.6 和 PhantomJS 1.7.0。任何帮助表示赞赏。

【问题讨论】:

    标签: requirejs phantomjs mocha.js yeoman


    【解决方案1】:

    我通过在test/index.html 中仔细检查mocha 的路径解决了同样的警告

    <link rel="stylesheet" href="lib/mocha/mocha.css">
    </head>
    <body>
      <div id="mocha"></div>
      <script src="lib/mocha/mocha.js"></script>
    

    【讨论】:

    • 我仍然在 html 中引用了mocha.js,它并没有为我改变它。我的项目不使用主干。
    • 在我的例子中,我假设 yeoman 在我运行原始 webapp 生成器时会安装 Mocha。在检查了我的索引路径及其指向的位置后,我发现它丢失了。 bower install mocha 把我搞定了。
    【解决方案2】:

    你在 Gruntfile 中的 mocha 配置是什么。你有没有类似的东西:

    mocha: {
      all: ['http://localhost:3501/index.html']
    },
    

    【讨论】:

    猜你喜欢
    • 2012-11-21
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 2015-11-01
    相关资源
    最近更新 更多