javascript 当我运行 mocha 测试时,我总是收到错误:连接 ECONNREFUSED
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22585877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When I run mocha tests I always get Error: connect ECONNREFUSED
提问by Topicus
I'm trying to run my app tests with mocha and supertest but I always get Error: connect ECONNREFUSED.
我正在尝试使用 mocha 和 supertest 运行我的应用程序测试,但我总是收到错误:连接 ECONNREFUSED。
app.js code:
app.js 代码:
var express = require('express'),
mongoose = require('mongoose'),
fs = require('fs'),
config = require('./config/config');
mongoose.connect(config.db);
var db = mongoose.connection;
db.on('error', function () {
throw new Error('unable to connect to database at ' + config.db);
});
var modelsPath = __dirname + '/app/models';
fs.readdirSync(modelsPath).forEach(function (file) {
if (file.indexOf('.js') >= 0) {
require(modelsPath + '/' + file);
}
});
var app = express();
require('./config/express')(app, config);
require('./config/routes')(app);
app.listen(config.port);
exports.app = app;
console.log('LISTEN ON http://localhost:3000/')
test.js code:
test.js 代码:
var should = require('should');
var assert = require('assert');
var request = require('supertest');
var mongoose = require('mongoose');
var app = require('../../../app').app;
describe('Categories', function() {
it('- POST is testing', function(done) {
request(app)
.post('http://localhost:3000/categories')
.send({title:'test', text:'test'})
.end(function(e, res) {
console.log(e, res);
});
});
});
Test output:
测试输出:
LISTEN ON http://localhost:3000/
Categories
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' } undefined
1) - POST is testing
0 passing (2s)
1 failing
Done, but with warnings.
Could you give any clue why this happend?
你能提供任何线索为什么会发生这种情况吗?
Thank you in advance!
先感谢您!
Topicus
话题
回答by Topicus
The problem was the path. Replacing "localhost:3000/categories" by this "/categories" all works fine.
问题是路径。用这个“/categories”替换“localhost:3000/categories”一切正常。
Working example:
工作示例:
describe('Categories', function() {
it('- POST is testing', function(done) {
request(app)
.post('/categories')
.send({title:'test', text:'test'})
.end(function(e, res) {
console.log(e, res);
});
});
});
回答by Mahfuzur Rahman
I'm facing same problem, and find out the problem is- only the path url. Path should be in this way -
我面临同样的问题,发现问题是 - 只有路径 url。路径应该是这样的——
.post('/categories')