我是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);
});
});

你能否解释为何会发生这种情况? 谢谢
答案 0 :(得分:-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);
});
});
});