2016-08-18 50 views
0

我正在用AJAX POST并试图测试发送的数据,但我得到错误“paramString.split不是一个函数”当测试运行时。我已经找过关于这个的其他文章,但他们似乎都是为了让FormData与AJAX一起工作,而我并没有遇到麻烦。数据发送,但我不能写一个成功的测试。不能停止试图解析FormData对象的茉莉花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 
    }); 
} 

测试:

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 
    }); 

错误

TypeError: paramString.split is not a function 

误差在该线中发生: https://github.com/jasmine/jasmine-ajax/blob/master/src/paramParser.js#L18。我在那里放了一个断点,paramString实际上是一个FormData对象。我假设用jasmine.Ajax.install嘲笑请求覆盖了原始请求中的processData: false,但我不确定如何解决该问题。

回答

0

我不得不改变测试是

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