简体   繁体   English

无法阻止jasmine-ajax尝试解析FormData对象

[英]Can't stop jasmine-ajax from trying to parse FormData object

I'm making a POST with AJAX and trying to test the data that gets sent, but I'm getting the error "paramString.split is not a function" when that test runs. 我正在使用AJAX进行POST,并尝试测试发送的数据,但是在该测试运行时出现错误“ paramString.split不是函数”。 I've looked for other posts about this but they all seem to be about getting FormData to work with AJAX, which I'm not having trouble with. 我一直在寻找有关此的其他文章,但它们似乎都是关于让FormData与AJAX一起使用的,我对此没有遇到任何麻烦。 The data sends, but I can't write a successful test around it. 数据已发送,但我无法围绕它编写成功的测试。

AJAX: AJAX:

upload: (file, progressCallback) => {
  let data = new FormData();
  data.append('image', file);

  return $.ajax({
    xhr: function() {
      let xhr = new window.XMLHttpRequest();

      xhr.upload.addEventListener('progress', progressCallback);
      return xhr;
    },
    method: 'POST',
    url: apiUrl(),
    cache: false,
    processData: false,
    contentType: false,
    data: data
  });
}

Test: 测试:

describe('my test', () => {

  beforeEach(function() {
    jasmine.Ajax.install()
  });

  afterEach(function() {
    jasmine.Ajax.uninstall();
  });

  it('sends a POST request to the right endpoint with data', function() {
    const image = { 
        size: 10000,
        type: 'image/jpeg'
    };

    let data = new FormData();
      data.append('image', image);

    myService.upload(image); // POST happens here

    const request = jasmine.Ajax.requests.mostRecent();

    expect(request.method).toBe('POST');              // passes
    expect(request.url).toBe('/dashapi/dam-assets/'); // passes
    expect(request.data()).toEqual(data);             // error
  });

Error 错误

TypeError: paramString.split is not a function

The error is occurring at this line: https://github.com/jasmine/jasmine-ajax/blob/master/src/paramParser.js#L18 . 错误发生在此行: https : //github.com/jasmine/jasmine-ajax/blob/master/src/paramParser.js#L18 I put a breakpoint there and paramString is actually a FormData object at that point. 我在此处放置了一个断点,而paramString实际上是一个FormData对象。 I'm assuming that mocking out the request with jasmine.Ajax.install is overwriting the processData: false I have in the original request, but I'm not sure how to fix that. 我假设用jasmine.Ajax.install请求将覆盖processData: false我在原始请求中拥有,但是我不确定如何解决该问题。

我不得不将测试更改为

expect(request.params).toEqual(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM