Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得Sinon';在Mocha和Node中时,是否让fakeServer响应jQuery.ajax调用?_Javascript_Jquery_Node.js_Mocha.js_Sinon - Fatal编程技术网

Javascript 如何获得Sinon';在Mocha和Node中时,是否让fakeServer响应jQuery.ajax调用?

Javascript 如何获得Sinon';在Mocha和Node中时,是否让fakeServer响应jQuery.ajax调用?,javascript,jquery,node.js,mocha.js,sinon,Javascript,Jquery,Node.js,Mocha.js,Sinon,我在这里看到了很多答案,在那里看到了博客帖子,在Sinon自己的问题追踪中看到了问题,但没有找到任何明确的答案。到目前为止,我大致了解到: import XMLHttpRequest from "xhr2"; ... beforeEach((done) => { jsdom.env("<!doctype html><html><body><div></div></body></html>", (erro

我在这里看到了很多答案,在那里看到了博客帖子,在Sinon自己的问题追踪中看到了问题,但没有找到任何明确的答案。到目前为止,我大致了解到:

import XMLHttpRequest from "xhr2";
...
beforeEach((done) => {
  jsdom.env("<!doctype html><html><body><div></div></body></html>", (error, window) => {
    for (var i in require.cache) {delete require.cache[i];}

    Object.assign(global, _.pick(window, "document", "navigator"));
    global.window = window;
    window.console = global.console;
    document.body.dataset = {};

    ...

    global.$ = require("jquery");
    global.$(() => {
      global.$.support.cors = true;
      global.$.ajaxSettings.xhr = () => {return new XMLHttpRequest();};
      const fakeServer = require("sinon/lib/sinon/util/fake_server").fakeServer;
      global.server = fakeServer.create({autoRespond: true});
      done();
    });
  });
});

...

it("should bloody work!", () => {
  server.respondWith([401, {}, ""]);
  ...
  [some test code that triggers an $.ajax call];
});
无论我是将假服务器设置为使用200和“OK”来响应,还是处理.ajax调用,都没有区别,所有道路都会导致状态为0和文本为“Error”

jqXHR = { 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: 'Error' }
textStatus = error
errorThrown = [Error]