Mongo 连接到应用程序时出现“连接被拒绝”错误
Connection Refused error when Mongo is connected to application
我正在用 Mocha 编写一些测试,我的第一个测试总是通过:
const assert = require('assert');
const request = require('supertest');
const app = require('../app');
describe('The express app', () => {
it('handles a GET request to /api', done => {
request(app)
.get('/api')
.end((err, response) => {
assert(response.body.hi === 'there');
done();
});
});
});
但是这第二个测试从一开始就总是失败:
const assert = require("assert");
const request = require("supertest");
const mongoose = require("mongoose");
const app = require("../../app");
const Driver = mongoose.model("driver");
describe("Drivers controller", () => {
it("Post to /api/drivers create a new driver", () => {
let oldCount;
return Driver.count()
.then(count => {
oldCount = count;
return new Promise((resolve, reject) => {
request(app)
.post("api/drivers")
.send({ email: "test@test.com" })
.end((err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
})
.then(() => {
return Driver.count();
})
.then(newCount => {
assert(oldCount + 1 === newCount);
});
});
});
以上是它的第三次重构,我正在测试这个控制器:
const Driver = require("../models/driver");
module.exports = {
greeting(req, res) {
res.send({ hi: "there" });
},
create(req, res) {
console.log(req.body);
const driverProps = req.body;
Driver.create(driverProps).then(driver => res.send(driver));
}
};
在最初的重构中,我发现 assert(oldCount + 1 === newCount);
返回的是 falsy
而不是 truthy
,这出乎我的意料,并且在我的测试重构中,连接被拒绝,但是数据库已连接我检查了这个配置:
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const routes = require("./routes/routes");
const app = express();
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/muber", { useMongoClient: true });
const connection = mongoose.connection;
connection.on("connected", function() {
console.log("connected to db");
});
app.use(bodyParser.json());
routes(app);
module.exports = app;
结果:
[nodemon] starting mocha --recursive -R min
connected to db
1 passing (43ms) 1 failing
1) Drivers controller Post to /api/drivers create a new driver:
Error: ECONNREFUSED: Connection refused
at Test.assert (node_modules/supertest/lib/test.js:164:13)
at Server.assert (node_modules/supertest/lib/test.js:131:12)
at emitCloseNT (net.js:1600:8)
at processTicksAndRejections (internal/process/next_tick.js:76:17)
[nodemon] app crashed - waiting for file changes before starting...
不确定发生了什么。
天哪,我不得不停止测试并停止 mongo 数据库服务器并重新启动它然后 运行 测试,一切都通过并按预期工作:
connected to db
{ email: 'test@test.com' }
2 passing (132ms)
[nodemon] clean exit - waiting for changes before restart
重构实际上不是必需的,但当我把它放回时我确实做了一些调整:
describe("Drivers controller", () => {
it("Post to /api/drivers create a new driver", done => {
Driver.count().then(count => {
request(app)
.post("/api/drivers")
.send({ email: "test@test.com" })
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200)
.end(() => {
Driver.count().then(newCount => {
assert(count + 1 === newCount);
done();
});
});
});
});
});
我正在用 Mocha 编写一些测试,我的第一个测试总是通过:
const assert = require('assert');
const request = require('supertest');
const app = require('../app');
describe('The express app', () => {
it('handles a GET request to /api', done => {
request(app)
.get('/api')
.end((err, response) => {
assert(response.body.hi === 'there');
done();
});
});
});
但是这第二个测试从一开始就总是失败:
const assert = require("assert");
const request = require("supertest");
const mongoose = require("mongoose");
const app = require("../../app");
const Driver = mongoose.model("driver");
describe("Drivers controller", () => {
it("Post to /api/drivers create a new driver", () => {
let oldCount;
return Driver.count()
.then(count => {
oldCount = count;
return new Promise((resolve, reject) => {
request(app)
.post("api/drivers")
.send({ email: "test@test.com" })
.end((err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
})
.then(() => {
return Driver.count();
})
.then(newCount => {
assert(oldCount + 1 === newCount);
});
});
});
以上是它的第三次重构,我正在测试这个控制器:
const Driver = require("../models/driver");
module.exports = {
greeting(req, res) {
res.send({ hi: "there" });
},
create(req, res) {
console.log(req.body);
const driverProps = req.body;
Driver.create(driverProps).then(driver => res.send(driver));
}
};
在最初的重构中,我发现 assert(oldCount + 1 === newCount);
返回的是 falsy
而不是 truthy
,这出乎我的意料,并且在我的测试重构中,连接被拒绝,但是数据库已连接我检查了这个配置:
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const routes = require("./routes/routes");
const app = express();
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/muber", { useMongoClient: true });
const connection = mongoose.connection;
connection.on("connected", function() {
console.log("connected to db");
});
app.use(bodyParser.json());
routes(app);
module.exports = app;
结果:
[nodemon] starting
mocha --recursive -R min
connected to db1 passing (43ms) 1 failing
1) Drivers controller Post to /api/drivers create a new driver: Error: ECONNREFUSED: Connection refused at Test.assert (node_modules/supertest/lib/test.js:164:13) at Server.assert (node_modules/supertest/lib/test.js:131:12) at emitCloseNT (net.js:1600:8) at processTicksAndRejections (internal/process/next_tick.js:76:17)
[nodemon] app crashed - waiting for file changes before starting...
不确定发生了什么。
天哪,我不得不停止测试并停止 mongo 数据库服务器并重新启动它然后 运行 测试,一切都通过并按预期工作:
connected to db
{ email: 'test@test.com' }
2 passing (132ms)
[nodemon] clean exit - waiting for changes before restart
重构实际上不是必需的,但当我把它放回时我确实做了一些调整:
describe("Drivers controller", () => {
it("Post to /api/drivers create a new driver", done => {
Driver.count().then(count => {
request(app)
.post("/api/drivers")
.send({ email: "test@test.com" })
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200)
.end(() => {
Driver.count().then(newCount => {
assert(count + 1 === newCount);
done();
});
});
});
});
});