我使用AJAX创建了一个POST,并尝试测试发送的数据,但在测试运行时收到错误消息"paramString.split不是一个函数“。我还在寻找其他关于这方面的帖子,但它们似乎都是关于让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,但我不确定如何解决这个问题。
发布于 2016-08-20 01:38:51
我不得不把考试改成
expect(request.params).toEqual(data);https://stackoverflow.com/questions/39027933
复制相似问题