javascript 如何使用 Qunit 将 HTML 固定装置与 Karma 测试运行器一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16209749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use HTML fixtures with Karma test runner using Qunit?
提问by daveoncode
I'm playing with Karmatest 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:
我正在使用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}
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:
假设我的装置是一个简单的 html 文件,其中包含以下标记:
<div id="container">hello world</div>
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??
如何编写可以访问该节点(div)的测试?据我所知,“文档”与“静态”文件夹下的“context.html”文件有关......那么我的夹具的HTML在哪里?
采纳答案by daveoncode
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:
我没有使用 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}
];
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();
}
});
回答by Vojta
If you are using AngularJS, you can use the html2js preprocessor.An example how to do that is at https://github.com/vojtajina/ng-directive-testing.
如果您使用的是 AngularJS,则可以使用 html2js 预处理器。如何做到这一点的一个例子是在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-preprocessorYou 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+(目前在金丝雀频道),所以你必须使用金丝雀(它包含很多变化;-))...