gpt4 book ai didi

javascript - 当我运行 Mocha 测试时,我总是收到错误 : connect ECONNREFUSED

转载 作者:行者123 更新时间:2023-12-03 03:42:57 25 4
gpt4 key购买 nike

我正在尝试使用 mocha 和 supertest 运行我的应用程序测试,但总是收到错误:connect ECONNREFUSED。

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代码:

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

测试输出:

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.

你能给出任何线索为什么会发生这种情况吗?

提前谢谢您!

主题

最佳答案

问题出在路径上。将“localhost:3000/categories”替换为“/categories”一切正常。

工作示例:

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

关于javascript - 当我运行 Mocha 测试时,我总是收到错误 : connect ECONNREFUSED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22585877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com