Javascript Sinon.JS错误无效URL,如何修复
我尝试了很多次,但总是不起作用。 我使用Mocha单元测试。 这是我的代码单元测试:Javascript Sinon.JS错误无效URL,如何修复,javascript,unit-testing,mocha.js,sinon,Javascript,Unit Testing,Mocha.js,Sinon,我尝试了很多次,但总是不起作用。 我使用Mocha单元测试。 这是我的代码单元测试: var expect = require("expect"); var jQuery = require("jquery"); var sinon = require("sinon"); describe("SinonFakeServerWithJasmine", function() { var server; beforeEach(function() { ser
var expect = require("expect");
var jQuery = require("jquery");
var sinon = require("sinon");
describe("SinonFakeServerWithJasmine", function() {
var server;
beforeEach(function() {
server = sinon.fakeServer.create();
});
afterEach(function() {
server.restore();
});
it("should fake a jQuery ajax request", function() {
server.respondWith("GET", "/something", [200, {
"Content-Type": "application/json"
},
'{ "stuff": "is", "awesome": "in here" }'
]);
jQuery.ajax({
url: "/something",
success: function() {
console.log("success");
},
error: function(err) {
console.log(err);
}
});
server.respond(); // Process all requests so far
});
}))
==>这是日志错误Jquery.ajax
{ readyState: 0,
getResponseHeader: [Function],
getAllResponseHeaders: [Function],
setRequestHeader: [Function],
overrideMimeType: [Function],
statusCode: [Function],
abort: [Function],
state: [Function],
always: [Function],
then: [Function],
promise: [Function],
pipe: [Function],
done: [Function],
fail: [Function],
progress: [Function],
complete: [Function],
success: [Function],
error: [Function],
status: 0,
statusText: 'TypeError: Invalid URL' }
我不知道为什么会这样。我不使用expect,因为它在我的情况下是不必要的,我想用摩卡测试sinon
感谢大家花时间阅读。我在使用Jasmine时遇到了同样的无效URL问题 我不得不使用
XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
jQuery.ajaxSettings.xhr = function() {
return new XMLHttpRequest();
};