javascript - 如何使用 Qunit 将 HTML 装置与 Karma 测试运行器结合使用?

标签 javascript unit-testing tdd qunit karma-runner

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

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

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

假设我的装置是一个简单的 html 文件,包含以下标记:

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

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

最佳答案

我没有使用 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();
    }
});

关于javascript - 如何使用 Qunit 将 HTML 装置与 Karma 测试运行器结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16209749/

相关文章:

javascript - angularjs中部分之间的2向数据绑定(bind)

javascript - 无法创建以数组索引作为键和值的 JS 对象?

python - 使用 HTML 或 Textile 自定义 Python 单元测试报告

c++ - Qt单元测试输出字符串长度截断

c# - 在创建用户时测试 ASP.NET 成员资格提供程序重复电子邮件

c# - 对接口(interface)存储库进行单元测试的目的是什么

c# - .net 是否根据测试方法创建实例?

javascript - Serial Chart 每天有多个值? (美图)

javascript - 支付成功页面AngularJS

unit-testing - Selenium Java 测试用例不工作