0
在一个组件中,我发送了一个xhr请求到'/ api/somecall'如何用JS测试的绝对路径来模拟请求?
我试图用MochaJS来模拟用于测试目的的HTTP请求。
我进入了nock,但第一个参数是域,由于我们在多个域中使用相同的应用程序,所以无法修复。
我虽然关于用location.href模拟一个DOM,所以xhr会在Mocha的测试会话的开始时以这种方式发送到一个特定的域中(我发现这个代码的一部分在web ):
nock('http://localhost')
.get('/apv/v2/listings/' + county)
.reply(200, responseData)
所有的测试返回的请求tiemout像以前一样:
// setup the simplest document possible
var doc = jsdom.jsdom('<!doctype html><html><body></body></html>', {url: "http://localhost"})
// get the window object out of the document
var win = doc.defaultView
// set globals for mocha that make access to document and window feel
// natural in the test environment
global.document = doc
global.window = win
// take all properties of the window object and also attach it to the
// mocha global object
propagateToGlobal(win)
// from mocha-jsdom https://github.com/rstacruz/mocha-jsdom/blob/master/index.js#L80
function propagateToGlobal (window) {
for (let key in window) {
if (!window.hasOwnProperty(key)) continue
if (key in global) continue
global[key] = window[key]
}
}
不幸的是,仍然没有当我尝试这种事后工作。
我怎么可能得到它的工作?