correct usage of sinon's fake XMLHttpRequest

半城伤御伤魂 提交于 2019-12-05 10:23:25

Because you are running this outside of a browser environment there is no XMLHttpRequest object. Since your are mocking it with Sinon what you can do is declare a fake global function in your beforeEach call.

global.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
timhc22

I did this for overriding XMLHttpRequest (see my question and answer here):

var FakeXMLHTTPRequests = require('fakexmlhttprequest')
var requests   = []

XMLHttpRequest = function() {
    var r =  new FakeXMLHTTPRequests(arguments)
    requests.push(r)
    return r
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!