sinonjs / sinon

Test spies, stubs and mocks for JavaScript.
https://sinonjs.org/
Other
9.63k stars 770 forks source link

sandbox.useFakeXMLHttpRequest is server instead of xhr #393

Closed Krinkle closed 9 years ago

Krinkle commented 10 years ago

This seems rather confusing, was this on purpose or is this a bug?

lib/sinon/sandbox.js#L120:

    sinon.sandbox.useFakeXMLHttpRequest = sinon.sandbox.useFakeServer;

As a result, code like the following (based on sinon-qunit documentation) doesn't actually work:

QUnit.test('example', function (assert) {
    var xhr = this.sandbox.useFakeXMLHttpRequest(),
        requests = [];

    xhr.onCreate = function (request) {
        requests.push(request);
    };

    $.getJSON('/test');
    assert.equal(this.requests.length, 1);

    requests[0].respond(200, { 'Content-Type': 'application/json' }, '{}');
});

.. because that attaches a spurious onCreate method to the server object. The fake xhr manager is actually in xhr.xhr and the code is fixed when changed to xhr.xhr.onCreate = function () { .. }.

n1k0 commented 10 years ago

Stumbled upon the same issue. Would be really nice to fix the API, or at the very least to update the docs…

jtruelove commented 10 years ago

I am seeing something similar

cjohansen commented 10 years ago

That does indeed look strange. I can't really remember the reasoning behind this, but I am open to fix it. Sinon has 80 contributors, more are always welcome ;)

renatewr commented 10 years ago

Hi! Is ther any new version fixing this issue?

gabrieledarrigo commented 8 years ago

Hi, even the new version use this:

sinon.sandbox.useFakeXMLHttpRequest = sinon.sandbox.useFakeServer;

?