简体   繁体   English

如何使用Qunit的Karma测试运行器使用HTML装置?

[英]How can I use HTML fixtures with Karma test runner using Qunit?

I'm playing with Karma test runner ( http://karma-runner.github.io/0.8/index.html ) using qunit ( http://qunitjs.com ). 我正在使用qunithttp://qunitjs.com )与Karma测试运行器( http://karma-runner.github.io/0.8/index.html )一起玩。 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. 我成功创建并运行了简单的测试(100%JavaScript),但现在我正在尝试使用HTML fixture来测试与DOM节点交互的代码。 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}

it get served by karma's server, but I don't understand how can I access to its DOM :( 它得到了karma的服务器,但我不明白我怎么能访问它的DOM :(

Let's suppose my fixture is a simple html file containing the following markup: 假设我的fixture是一个包含以下标记的简单html文件:

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

How can I write a test that can access to that node (the div)? 如何编写可以访问该节点(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?? 据我所知,“文档”与“静态”文件夹下的“context.html”文件有关...那么我的夹具的HTML在哪里?

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). 我没有使用AngularJS ......我通过采用jasmine-jquery来解决: https//github.com/velesin/jasmine-jquery (我只使用jasmine用于灯具,我的测试仍然使用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();
    }
});

If you are using AngularJS, you can use the html2js preprocessor. 如果您使用的是AngularJS,则可以使用html2js预处理器。 An example how to do that is at https://github.com/vojtajina/ng-directive-testing . 如何做到这一点的一个例子是https://github.com/vojtajina/ng-directive-testing

These html files are served by Karma, but they are not included in the page, so you would have to fetch them - probably through xhr request. 这些html文件由Karma提供,但它们不包含在页面中,因此您必须获取它们 - 可能是通过xhr请求。

Here is a similar preprocessor, that converts html file into a JS string (not tight to Angular): https://github.com/karma-runner/karma-html2js-preprocessor You can see how to use it in the e2e test: https://github.com/karma-runner/karma-html2js-preprocessor/tree/master/e2e-test 这是一个类似的预处理器,它将html文件转换为JS字符串(不是Angular): https//github.com/karma-runner/karma-html2js-preprocessor您可以在e2e测试中看到如何使用它: https://github.com/karma-runner/karma-html2js-preprocessor/tree/master/e2e-test

NOTE: this html2js preprocessor is not part of Karma 0.8 and plugins only work with Karma 0.9+ (currently in canary channel), so you have to use canary (which contains lot of changes ;-))... 注意:这个html2js预处理器不是Karma 0.8的一部分,插件只适用于Karma 0.9+(目前在canary通道中),所以你必须使用canary(包含很多变化; - ))...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM