Friday 15 May 2015

node.js - How to call a fakeServer in Sinon.JS / Node -



node.js - How to call a fakeServer in Sinon.JS / Node -

i'm struggling figure out how utilize sinon false server in unit tests.

the illustration in documentation is:

setup: function () { this.server = sinon.fakeserver.create(); }, "test should fetch comments 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, don't know what's going on in mylib.getcommentsfor(...), can't tell how nail server.

so in node, i'm trying 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 want real server, how connect false one?

sinon overriding browser's xmlhttprequest create fakexmlhttprequest. you'll need find node xhr wrapper such https://github.com/driverdan/node-xmlhttprequest have sinon intercept calls code.

node.js unit-testing sinon

No comments:

Post a Comment