我试图运行我的应用程序测试与摩卡和最高,但我总是得到错误: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.
你能给出任何线索为什么发生吗?
先谢谢你!
Topicus
解决方法
问题是路径.这个“/ categories”替换“localhost:3000 / categories”一切正常.
工作实例:
describe('Categories',function(done) { request(app) .post('/categories') .send({title:'test',res); }); }); });