2014-11-06 51 views
4

我正在努力弄清楚如何在单元测试中使用sinon伪装服务器。如何在Sinon.JS/Node中调用fakeServer

的例子他们的文档中:

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仍然认为我想一个真正的服务器,让我怎么连接到假的?

回答