Jasmine / Karma无法正确加载Fixture HTML

问题描述

我创建了一个简单的Fixture HTML和Jasmine测试。

   <div id="testid">A</div>

这是我的测试代码

define(['jquery'],function($) {
    'use strict';

    describe("Jasmine Fixture Test",function() {

        beforeAll(function(done) {
            jasmine.Ajax.install();

            done();
        });

        afterall(() => {
            jasmine.Ajax.uninstall();
        });

        beforeEach(function() {
        });

        it("Test Fixture",function(done) {
            loadFixtures('TestFixture.html');

            expect($('#jasmine-fixtures')).toHaveId('jasmine-fixtures');
            expect($( '#testid' )).toHaveId('testid');
            expect($( '#testid' )).toHaveText('A');
            expect(true).toBeTruthy();
            done();
        });
    });
});

调用loadFixture时没有错误

我已将html夹具文件添加到以下目录:

C:\Workspace\PLAN_BUILDER\branches\634623.1.0.0\spec\javascripts\fixtures

我正在从运行“ gradlew测试”

C:\Workspace\PLAN_BUILDER\branches\634623.1.0.0

我将调试语句添加到karma-jasmine-jquery库中,如下所示:

  jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) {
      console.log(relativeUrl);
      console.log(this.makeFixtureUrl_(relativeUrl));
    var self = this,url = this.makeFixtureUrl_(relativeUrl),htmlText = '',request = $.ajax({
        async: false,// must be synchronous to guarantee that no tests are run before fixture is loaded
        cache: false,url: url,timeout: 2000,success: function (data,status,$xhr) {
            
            console.log(data);
            console.log(status);
            console.log($xhr.responseText);
          htmlText = $xhr.responseText
        }
      }).fail(function ($xhr,err) {
          console.log(err);
          console.log(status);
          throw new Error('Fixture Could not be loaded: ' + url + ' (status: ' + status + ',message: ' + err.message + ')')
      }).always(function() {
    alert( "complete" );
  }
      );
console.log("after");
console.log(htmlText);

...

输出是这样的:

LOG LOG: 'TestFixture.html'
LOG LOG: 'spec/javascripts/fixtures/TestFixture.html'
LOG LOG: 'after'
LOG LOG: ''

因此从$ .ajax调用中都不会调用成功或失败方法回调,并且htmlText为空。

感谢您的建议。

解决方法

问题是我在打电话

jasmine.Ajax.install();

之前

loadFixtures('TestFixture.html');

我不知道jasmine-jquery正在使用$ .ajax调用从磁盘加载夹具html文件。显然,然后茉莉花ajax取代了$ .ajax行为。

谢谢。