【问题标题】:Error: connect ECONNREFUSED , mocha , supertest错误:连接 ECONNREFUSED 、 mocha 、 supertest
【发布时间】:2017-04-14 22:52:09
【问题描述】:

我是 mocha 和 supertest 的新手,我正在尝试测试我的 API,但总是收到连接被拒绝错误。

  
var request = require('supertest');

 it("posts a new comment to /comment", function (done) {
    var comment = { username: 'riheb', userId: 'test1', userPhotoId: '12a', comment: 'update file  now' };

    request("http://localhost:3000")
      .post("/sp/place/582f148515035818e080e653/folder/582f16b3ef9caf029863331b/file/583d6b5243af5628b8491fd3/comment")
      .send(comment)
      .expect(200)
      .expect(" riheb comment is stored", done);
  });
});

你能提供任何线索为什么会发生这种情况吗? 谢谢

【问题讨论】:

    标签: node.js mocha.js supertest


    【解决方案1】:

    如果你想使用supertest 库,你必须公开和注入你的应用程序(express)而不是一个网址(因为你不会有一个活跃的服务器监听):

    var request = require('supertest');
    var app = require('../yourappexposed');
    
    describe("Comments", function() {
     it("posts a new comment to /comment", function (done) {
        var comment = { username: 'riheb', userId: 'test1', userPhotoId: '12a', comment: 'update file  now' };
        request(app)
          .post("/sp/place/582f148515035818e080e653/folder/582f16b3ef9caf029863331b/file/583d6b5243af5628b8491fd3/comment")
          .send(comment)
          .expect(200)
          .expect(" riheb comment is stored", done);
        });
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2020-02-02
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 2019-03-15
      • 2015-07-27
      • 2014-05-20
      相关资源
      最近更新 更多