【问题标题】:Fixtures won't load in Karma固定装置不会在 Karma 中加载
【发布时间】:2014-10-02 01:35:56
【问题描述】:

我已经尝试了很多东西,但是当我运行 Karma 时,我似乎无法加载固定装置。我尝试过使用html2jskarma-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() 开始,beforeEach() 运行以下两行:

jasmine.getFixtures().fixturesPath = '../';
jasmine.getFixtures().load('/karma/imagelist.fix.html');

然后it() 函数启动。他们正在测试一个使用document.getElementById() 向一些<img> 元素添加事件监听器的函数。这就是夹具需要进入的地方,因为没有它getElementById() 将返回null。 (这是我遇到的问题。)

我已经在网上搜索了将近一整天来寻找可以尝试的东西,但除了TypeError: document.getElementById(...) is null 之外,我似乎无法从 Karma 得到任何东西。我已经尝试更改我的 Karama 配置,以便 preprocessors 在其中包含 "**/*.html": [] 以禁用 html2js,而不是为空,但这没有任何作用。我尝试禁用jasmine-jquery 并改用html2js,但karma-html2js-preprocessor 上几乎没有任何文档,所以我什至无法判断我是否正确使用它。 (这就是为什么我更倾向于jasmine-jquery。)我就是想不通。

更新(10 月 3 日)

我找到了this question on Stack Overflow 并在那里尝试了答案,但没有奏效——我仍然遇到了我一直看到的相同行为。我的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');

然后我找到this issue thread on GitHub 并将karma.conf.js 中的preprocessors 更改为:

preprocessors: {
    '**/*.html': []
},

我将规范中的 Jasmine 调用更改为:

var fixtures = jasmine.getFixtures();
fixtures.fixturesPath = 'base/karma/';
fixtures.load('imagelist.fix.html');

这也导致了相同的行为:TypeError: document.getElementById(...) is null

我还找到了this post 并尝试了其中列出的配置——除了在karma.conf.jsfiles 部分添加了JASMINEJASMINE_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 日)

我找到了this question/answer on StackOverflow 并尝试根据其中的解决方案使用html2js 进行设置。我从karma.conf.jsframeworks 部分中删除了jasmine-jquery,并添加了一个如下所示的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

【问题讨论】:

  • 这就是你的提问方式。

标签: javascript gruntjs jasmine karma-runner fixtures


【解决方案1】:

如果您使用karma-jasmine-jquery,您只需设置以下内容:

  1. 将其添加为框架 - frameworks: ['jasmine-jquery', 'jasmine']

  2. 将您的固定装置作为模式包含在 files 数组下 - { pattern: 'test/fixtures/*.html', included: false, served: true }

  3. 在您的测试套件中使用base/ 设置路径,例如jasmine.getFixtures().fixturesPath = 'base/test/fixtures';

  4. 将夹具加载到您的测试规范中 - loadFixtures('index.html');

业力-茉莉花-jquery https://www.npmjs.com/package/karma-jasmine-jquery

业力配置 http://karma-runner.github.io/0.12/config/files.html

更新 02.16.16

较新版本的 Chrome 不允许 file:// URI 读取其他 file:// URI。实际上,jasmine-jquery 无法在某些版本的 Chrome 下正确加载固定装置。对此的替代方法是使用开关 --allow-file-access-from-files 运行 Chrome。 https://github.com/velesin/jasmine-jquery#cross-domain-policy-problems-under-chrome

【讨论】:

  • 这是金子。我有一个构建过程,我已经从 Grunt 转移到 Gulp,并且在匹配功能方面真的很痛苦。最后,当我到达测试部分(与 Jasmine 一起)时,我在设置固定装置时遇到了问题。我曾经在 testRunner 中手动包含 jasmine-jquery 和 jquery。现在一切正常github.com/serbanghita/formToObject.js/blob/master/gulpfile.js
  • 酷!很高兴听到@serbanghita。干杯。
  • 还有!!值得一提的是,我必须添加 plugins : "karma-jasmine-jquery" 才能使其正常工作。谢谢@cjroe
【解决方案2】:

如何在 Karma 中加载 JSON Fixture?

Step-1 将 json 固定装置注册到 karma.conf.js

内的 files 数组中
// 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'

      ]

Step-2sample.spec.js 文件中的描述中加载 json fixture

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 中加载 HTML Fixture?

Step-1如下创建一个HTMLfixture(test.dom.js文件)

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 ******** \
\
       ');
}

Step-2 在文件数组内的 karma config 中注册电子邮件夹具,如下所示:

  files: [
         //load fixtures
         "tests/fixtures/test.dom.js",
        //load spec file
        './tests/emailclient.spec.js',
        ]

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

   });

  });

【讨论】:

    【解决方案3】:

    在 karma.conf.js 文件中向框架添加夹具为我解决了这个问题。 frameworks: ['jasmine', 'fixture']

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多