首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jasmine.ajax的模拟XMLHttpRequest给出了jasmine.ajax=未定义

使用jasmine.ajax的模拟XMLHttpRequest给出了jasmine.ajax=未定义
EN

Stack Overflow用户
提问于 2019-03-15 12:58:08
回答 1查看 867关注 0票数 0

我正在尝试使用jasmine在单元测试中模拟XMLHttpRequest。我使用Aurelia来生成我的应用程序,我使用Karma作为单元测试运行程序。

对于单元测试,我尝试导入jasmine:

代码语言:javascript
复制
import * as jasmine from 'jasmine-ajax';

describe('when calling the API', () => {
  beforeEach(() => {
    jasmine.Ajax.install();
  });

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

  it('then it should get an answer back', () => {
    var doneFn = jasmine.createSpy("success");

    var xhr= new XMLHttpRequest();
    xhr.onreadystatechange = function(args) {
      if(this.readyState == this.DONE) {
        doneFn(this.responseText);
      }
    };

    xhr.open("GET", "https://someaddress.net/api/plans");
    xhr.send();

    expect(jasmine.Ajax.requests.mostRecent().url).toBe("https://someaddress.net/api/plans");
    expect(doneFn).not.toHaveBeenCalled();

    jasmine.Ajax.requests.mostRecent().respondWith({
      "status":200,
      "contentType": 'text/plain',
      "responseText": 'awesome response'
    });

    expect(doneFn).toHaveBeenCalledWith('awesome response');

  });
});

在使用Karma进行测试时,我得到:

jasmine_ajax__WEBPACK_IMPORTED_MODULE___.jasmine :在测试/业力-bundle.js行3146 >TypeError(第7行)中未定义

我发现我已经安装了jasmine的类型定义。

npm @type/jasmine

然后,我删除了jasmine的import语句,这导致了以下错误:

TypeError:在测试/业力-bundle.js第3134行> jasmine.Ajax (第3行)中未定义jasmine.Ajax

那我做错什么了?

我用茉莉花3.3.9,茉莉花-ajax 3.4.0,业力4.0.1,打字本2.9.2,webpack 4.25,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-18 11:26:34

在安装了业力-jasmine (https://github.com/IDCubed/karma-jasmine-ajax)并在karma.conf.js中的框架数组中添加了“jasmine”之后,它就开始工作了。

代码语言:javascript
复制
module.exports = function(config) {
  config.set({
    frameworks: ['jasmine-ajax', 'jasmine']
  });
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55183168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档