我已经尝试了很多事情,但是当我运行Karma时,我似乎找不到一个固定设备来加载。我尝试过使用html2js和karma-jasmine-jquery--我倾向于使用后者,但只要我知道测试运行时它没有加载和附加到DOM中,那么无论哪一个工具加载到DOM中,我都不会介意)。
我的目录结构非常简单:
img/
↳ mac.png
↳ link.png
↳ pocketwatch.png
js/
↳ spriteloader.js
↳ spriteloader.spec.js
karma/
↳ imagelist.fix.html
↳ karma.conf.js
Gruntfile.coffee
index.html
grunt.config.coffee
package.json这些是我安装的节点模块:
grunt 0.4.5
grunt-contrib-jshint 0.10.0
grunt-contrib-watch 0.6.1
grunt-karma 0.9.0
karma 0.12.23
karma-chrome-launcher 0.1.4
karma-firefox-launcher 0.1.3
karma-html2js-preprocessor": "^0.1.0",
karma-jasmine 0.2.0
karma-jasmine-jquery 0.1.1
karma-safari-launcher 0.1.1
karma-story-reporter 0.2.2下面是我的karma.conf.js设置:
basePath: '../',
frameworks: ['jasmine-jquery', 'jasmine'],
files: [
// Source and spec files
{
pattern: 'js/*.js',
watched: true,
served: true,
included: true
},
// Fixtures
{
pattern: 'karma/*.fix.html',
watched: false,
served: true,
included: false
}
],
exclude: [
],
preprocessors: {
},
reporters: ['story'],
port: 9018,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome', 'Firefox', 'Safari'],
singleRun: true在我的规范中,我从一个运行以下两行的describe()开始:
jasmine.getFixtures().fixturesPath = '../';
jasmine.getFixtures().load('/karma/imagelist.fix.html');然后启动it()函数。他们正在测试一个使用<img>向某些document.getElementById()元素添加事件侦听器的函数。这是夹具需要进入的地方,因为没有它,getElementById()将返回null。(这就是我遇到的问题。)
我已经在网络上搜索了将近一整天,寻找可以尝试的东西,但是除了TypeError: document.getElementById(...) is null之外,我似乎无法从Karma获得任何东西。我已经尝试过更改我的Karama配置,使preprocessors不再是空的,而是在其中包含了"**/*.html": []来禁用html2js,但是这没有起到任何作用。我尝试过禁用jasmine-jquery并使用html2js,但是karma-html2js-preprocessor上几乎没有任何文档,所以我甚至无法判断我是否正确地使用了它。(这就是我更倾向于jasmine-jquery的原因。)我就是搞不懂这件事。
更新(10月3日)
我找到了关于堆栈溢出的问题,并在那里尝试了答案,但是它没有起作用--我的行为仍然和我看到的一样。从上面看,我的karma.conf.js没有变化,但是在我的spriteloader.spec.js中,我将Jasmine调用更改为:
jasmine.getFixtures().fixturesPath = 'http://localhost:9019/base/karma/';
jasmine.getFixtures().load('imagelist.fix.html');我也试过了,并得到同样的结果:
jasmine.getFixtures().fixturesPath = 'http://localhost:9019/base/karma/';
jasmine.getFixtures().load('http://localhost:9019/base/karma/imagelist.fix.html');然后我找到了GitHub上的这个问题线程,并将preprocessors在karma.conf.js中更改为:
preprocessors: {
'**/*.html': []
},我把我的规范中的茉莉花叫声改为:
var fixtures = jasmine.getFixtures();
fixtures.fixturesPath = 'base/karma/';
fixtures.load('imagelist.fix.html');这也导致了同样的行为:TypeError: document.getElementById(...) is null
我还找到了这个职位,并尝试了其中概述的配置--除了在karma.conf.js-but的files部分中添加了JASMINE和JASMINE_ADAPTER之外,它们仍然具有相同的行为。
由于我已经在本地安装了karma-jasmine-jquery,所以我指向了如下所示的jasmine-jquery脚本:
'node_modules/karma-jasmine-jquery/lib/jasmine-jquery.js'我甚至尝试完全放弃Jasmine调用,转而使用AJAX调用:
$('#fixture').remove();
$.ajax({
async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
dataType: 'html',
url: '../karma/imagelist.fix.html',
success: function(data) {
$('body').append($(data));
}
});不过我还是得到了同样的结果。不知道我还能尝试什么。
更新(10月4日)
我找到了这个问题/答案是关于StackOverflow的,并尝试将其设置为每个解决方案的html2js。我从我的jasmine-jquery的frameworks部分中删除了karma.conf.js,并添加了一个如下所示的plugins部分:
plugins: [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-html2js-preprocessor',
'karma-jasmine',
'karma-safari-launcher',
'karma-story-reporter'
],我还将preprocessors部分更改为如下所示:
preprocessors: {
'**/*.html': ['html2js']
},我将我的beforeEach()更改为:
beforeEach(function() {
var imagelist = __html__['../karma/imagelist.fix.html'];
document.body.appendChild(imagelist);
});不过,我还是看到了同一个TypeError: document.getElementById(...) is null。
发布于 2015-03-25 18:38:07
如果使用karma-jasmine-jquery,只需设置以下内容:
frameworks: ['jasmine-jquery', 'jasmine']{ pattern: 'test/fixtures/*.html', included: false, served: true }下面。base/ (如jasmine.getFixtures().fixturesPath = 'base/test/fixtures'; )设置路径。loadFixtures('index.html');中。karma-jasmine https://www.npmjs.com/package/karma-jasmine-jquery
Karma Config http://karma-runner.github.io/0.12/config/files.html
更新02.16.16
更新版本的Chrome不允许文件://URI读取其他文件://URI。实际上,jasmine无法在某些版本的Chrome下正确加载固定装置。对此的重写是使用开关--allow-file-access-from-files运行Chrome。https://github.com/velesin/jasmine-jquery#cross-domain-policy-problems-under-chrome
发布于 2018-08-03 07:21:09
如何在Karma?中加载JSON夹具
步骤-1注册json安装到karma.conf.js中的文件数组
// list of files / patterns to load in the browser
files: [
//load fixtures
{
pattern: 'tests/**/*.json',
watched: true,
served: true,
included: false
},
//Other js/spec files go here
'src/sample.test.js',
'tests/sample.test.spec.js'
]sample.spec.js文件中描述的Step-2加载json治具
describe('sample test suit', function () {
let testFixture = {};
//load test data fixture
beforeEach(function () {
//loading fixtures data before each case
jasmine.getJSONFixtures().fixturesPath = 'base/tests/fixtures';
testFixture = getJSONFixture('test.testdata.json').key;
});
it('should return test here', function () {
expect(testFixture ).toBe({ data : 'some data' });
});如何在Karma?中加载
(test.dom.js 步骤-1创建HTMLfixture,如下所示)
function setUpHTMLFixture() {
jasmine.getFixtures().set(' *********Email sender******** \
\
<div class="div3"> \
<a id="sender-0"> \
\
********* removeIncomingAttachmentsFromTab ******** \
<div id="newLineBR"></div> \
<div class="sender-attachments"></div> \
<div class="attachmentiFramecls" style="margin-left:10px;"></div> \
*********End removeIncomingAttachmentsFromTab ******** \
\
</a> \
</div> \
\
*********End email sender******** \
\
\
********* createIFrameFormForPost ******** \
\
<div id="iFrameFormForContainer"></div> \
\
*********End createIFrameFormForPost ******** \
\
\
********* initEmailBody ******** \
\
<div id="emailBody">Email Sending Test from test at 7/18/2018 8:06:56 AM \
hi, here is a a link http://testlabz.com/ \
This is an automated test email. \
Please do not reply</div> \
\
********* End initEmailBody ******** \
\
');
}步骤-2在文件数组中的业力配置中使用注册电子邮件,如下所示:
files: [
//load fixtures
"tests/fixtures/test.dom.js",
//load spec file
'./tests/emailclient.spec.js',
]步骤-2在每个测试用例之前加载HTMLFixture,如下所示:
describe('htmlFixture test suit', function () {
//load test dom fixture
beforeEach(function () {
//loading html fixtures data before each case
setUpHTMLFixture();
});
//test the element with here
it("should text html", function () {
expect($("#emailBody")).toExist();
});
});发布于 2018-08-09 19:21:44
将夹具添加到karma.conf.js文件中的框架解决了这个问题。frameworks: ['jasmine', 'fixture']
https://stackoverflow.com/questions/26153241
复制相似问题