【发布时间】:2014-11-06 22:51:07
【问题描述】:
我正在努力弄清楚如何在我的单元测试中使用 sinon 来伪造服务器。
他们文档中的示例是:
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" }]));
}
不幸的是,我不知道myLib.getCommentsFor(...) 中发生了什么,所以我不知道如何实际访问服务器。
所以在节点中,我正在尝试以下...
sinon = require('sinon');
srv = sinon.fakeServer.create();
srv.respondWith('GET', '/some/path', [200, {}, "OK"]);
http.get('/some/path') // Error: connect ECONNREFUSED :(
显然,http 仍然认为我想要一个真实的服务器,那我如何连接到假的?
【问题讨论】:
标签: node.js unit-testing sinon