我正在使用 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/