fakeServer not working
I have the following code for a simple fake server. I'm testing with Mocha:
import expect from 'expect';
import sinon from 'sinon';
import jQuery from 'jquery';
describe.only('sinonFakeServer', () => {
let server;
beforeEach(() => server = sinon.fakeServer.create());
afterEach(() => server.restore());
it('Should fake a jQuery ajax request', () => {
server.respondWith("GET", "/something",
[200, { "Content-Type": "application/json" },
'{ "stuff": "is", "awesome": "in here" }']);
let spy = sinon.spy();
jQuery.ajax({
url: "/something",
success: spy
});
server.respond();
expect(spy.called).toBe(true);
});
});
I get the following error:
Error: Expected false to be true
Obviously the spy isn't getting called, any idea why? I followed this Gist and the Fakeserver documentation on Sinon's website.
I guess sinon hasn't support for node.js for a while. At the issue https://github.com/sinonjs/sinon/issues/319, @mantoni say sinon 2.0 will support fake server in node.js.
I guess sinon hasn't support for node.js for a while. At the issue #319, @mantoni say sinon 2.0 will support fake server in node.js.
That is misquoting @mantoni, what he said in the linked issue was (emphasis mine):
Maybe this is something we could address properly in Sinon 2.0 by moving the XHR stuff into a separate module.
This was never intended to work in node, as stated here: https://github.com/sinonjs/sinon/issues/319#issuecomment-23244535
There are better ways to test your ajax code, than stubbing things at the very bottom of the stack. I would suggest stubbing out things as close to the source under test as possible.
Please post usage questions to the Sinon.JS mailinglist