如何使用Qunit的Karma测试运行器使用HTML装置? [英] How can I use HTML fixtures with Karma test runner using Qunit?

查看:213
本文介绍了如何使用Qunit的Karma测试运行器使用HTML装置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm playing with Karma test runner (http://karma-runner.github.io/0.8/index.html) using qunit (http://qunitjs.com). I succesfully created and ran simple tests (100% JavaScript), but now I'm trying to use HTML fixtures in order to test code that interacts with DOM nodes. I'm able to load these fixtures by declaring them in "files" in this way:

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

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

it get served by karma's server, but I don't understand how can I access to its DOM :(

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

Let's suppose my fixture is a simple html file containing the following markup:

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

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

How can I write a test that can access to that node (the div)? The "document" is related to the "context.html" file under "static" folder as far I know... so where are the HTML of my fixture??

推荐答案

我是不使用AngularJS ...我通过采用jasmine-jquery解决了: https://github.com/velesin/jasmine-jquery(我只使用茉莉花装置,我的测试仍然是wri使用qunit)。
在我的配置文件中,我有以下内容:

I'm not using AngularJS... I solved by adopting jasmine-jquery: https://github.com/velesin/jasmine-jquery (I use jasmine only for the fixtures, my tests are still written using qunit). In my configuration file I have the following:

    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}
    ];

然后在我的测试文件中:

then in my test files:

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();
    }
});

这篇关于如何使用Qunit的Karma测试运行器使用HTML装置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆