How to call fakeServer in Sinon.JS / Node

I'm struggling to figure out how to use Sinon to fake a server in my unit tests.

An example in their documentation:

    setUp: function () {
        this.server = sinon.fakeServer.create();
    },

    "test should fetch comments from server" : function () {
        this.server.respondWith("GET", "/some/article/comments.json",
            [200, { "Content-Type": "application/json" },
             '[{ "id": 12, "comment": "Hey there" }]']);

        var callback = sinon.spy();
        myLib.getCommentsFor("/some/article", callback);
        this.server.respond();

        sinon.assert.calledWith(callback, [{ id: 12, comment: "Hey there" }]));
    }

Unfortunately, I don’t know what is going on myLib.getCommentsFor(...), so I can’t say how to actually get to the server.

So, in node, I am trying to do the following ...

sinon = require('sinon');

srv = sinon.fakeServer.create();

srv.respondWith('GET', '/some/path', [200, {}, "OK"]);

http.get('/some/path') // Error: connect ECONNREFUSED :(

Obviously, http still thinks I want a real server, so how can I connect to a fake?

+4
source share
1 answer

Sinon XMLHttpRequest FakeXMLHttpRequest. node XHR, ​​ https://github.com/driverdan/node-XMLHttpRequest, Sinon .

0

All Articles