【问题标题】:How can I use HTML fixtures with Karma test runner using Qunit?如何使用 Qunit 将 HTML 固定装置与 Karma 测试运行器一起使用?
【发布时间】:2013-04-19 01:15:28
【问题描述】:

我正在使用 qunit (http://qunitjs.com) 与 Karma 测试运行器 (http://karma-runner.github.io/0.8/index.html) 一起玩。我成功地创建并运行了简单的测试(100% JavaScript),但现在我正在尝试使用 HTML 固定装置来测试与 DOM 节点交互的代码。我可以通过以这种方式在“文件”中声明它们来加载这些固定装置:

{pattern: 'fixtures/myfixture.html', watched: true, served: true, included: false}

它由 karma 的服务器提供服务,但我不明白如何访问它的 DOM :(

假设我的夹具是一个包含以下标记的简单 html 文件:

<div id="container">hello world</div>

如何编写可以访问该节点(div)的测试? 据我所知,“文档”与“静态”文件夹下的“context.html”文件有关……那么我的灯具的HTML在哪里??

【问题讨论】:

标签: javascript unit-testing tdd qunit karma-runner


【解决方案1】:

我没有使用 AngularJS...我通过采用 jasmine-jquery 解决了:https://github.com/velesin/jasmine-jquery(我仅将 jasmine 用于固定装置,我的测试仍然使用 qunit 编写)。 在我的配置文件中,我有以下内容:

    frameworks = ['qunit', 'jasmine'];

    files = [

      JASMINE, 
      JASMINE_ADAPTER,
      QUNIT, 
      QUNIT_ADAPTER,

      // dependencies
      {pattern: 'src/main/webapp/js/libs/jquery/jquery-1.8.3.js', watched: false, served: true, included: true},
      {pattern: 'src/test/js/lib/jasmine-jquery.js', watched: false, served: true, included: true},

      // fixtures
      {pattern: 'src/test/js/**/*.html', watched: true, served: true, included: false},
      {pattern: 'src/test/js/**/*.json', watched: true, served: true, included: false},
      {pattern: 'src/test/js/**/*.xml', watched: true, served: true, included: false},

      // files to test 
      {pattern: 'src/test/js/**/*.js', watched: true, served: true, included: true}
    ];

然后在我的测试文件中:

module("TestSuiteName", {
    setup: function() {
        var f = jasmine.getFixtures();
        f.fixturesPath = 'base';
        f.load('src/test/js/TestFixture.html');
    },
    teardown: function() {
        var f = jasmine.getFixtures();
        f.cleanUp();
        f.clearCache();
    }
});

【讨论】:

  • 干得好,伙计。你还需要在配置中包含 JASMINE 和 JASMINE_ADAPTER 吗?
  • 为了让它对我有用,我必须添加“预处理器:{'*/.html': [] }”。这是为了删除我在 Karma 0.10 中默认启用的 html2js 预处理器
  • 您是否可以提供一个示例 jsfiddle 或 jsbin? @daveoncode
  • 值得注意的是,预处理器路径是相对于 basePath 的,而 basePath 又是相对于 karma.conf 文件的位置。我的 karma.conf 在一个名为 test-config 的目录中,经过一些调试才发现我必须将 basePath 设置为“../”才能使预处理器工作
【解决方案2】:

如果您使用的是 AngularJS,则可以使用 html2js 预处理器。 如何做到这一点的一个例子是https://github.com/vojtajina/ng-directive-testing

这些 html 文件由 Karma 提供,但它们不包含在页面中,因此您必须获取它们 - 可能通过 xhr 请求。

这是一个类似的预处理器,将 html 文件转换为 JS 字符串(对 Angular 不严格):https://github.com/karma-runner/karma-html2js-preprocessor 您可以在 e2e 测试中看到如何使用它:https://github.com/karma-runner/karma-html2js-preprocessor/tree/master/e2e-test

注意:这个 html2js 预处理器不是 Karma 0.8 的一部分,插件仅适用于 Karma 0.9+(目前在金丝雀频道中),所以你必须使用金丝雀(其中包含很多变化;-)).. .

【讨论】:

  • 我也对使用 karma 测试 DOM 非常感兴趣。这会在未来的版本中提供,还是我应该寻求解决方法?我可能不会使用 Angular...
  • 只需使用 karma 的金丝雀版本(它最终会变得稳定;-) 并使用 karma-html2js-preprocessor 插件,它对 AngularJS 并不紧。
  • 如何在 npm 上安装金丝雀版本?抱歉这个愚蠢的问题。
  • 只是想说我发现你可以做到npm install karma@canary...我认为版本必须是升级才能工作的数字(我仍然不明白金丝雀版本是否发生变化和如果版本相同,会怎样)。另外,我想说这个要求很丑,karma 可以有一些简单的内置加载装置的机制。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多