Mongo连接到应用程序

时间:2019-05-27 15:57:23

标签: mongodb express mongoose mocha body-parser

我正在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]开始mocha --recursive -R min连接到数据库

     

1个通过(43ms)1个失败

     

1)驱动程序控制器发布到/ api / drivers中,以创建新的驱动程序:        错误:ECONNREFUSED:连接被拒绝         在Test.assert(node_modules / supertest / lib / test.js:164:13)         在Server.assert(node_modules / supertest / lib / test.js:131:12)         在emitCloseNT(net.js:1600:8)         在processTicksAndRejections(internal / process / next_tick.js:76:17)

     

[nodemon]应用程序崩溃-等待文件更改,然后再开始...

不确定发生了什么。

1 个答案:

答案 0 :(得分:0)

老牛,我不得不停止测试并停止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();
          });
        });
    });
  });
});