1

我在使用 Express 和 Sequelize 设置测试时遇到问题。我正在使用 Mocha + Chai 进行测试。我现在只是在尝试 ping。

server.js 代码:

const express = require('express');
const Sequelize = require('sequelize');
const bodyParser = require('body-parser');

const db = require('./config/db');

const app = express();
const router = express.Router();
const PORT = 8000;

//Use body parser for express
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

const sequelize = new Sequelize(db.database, db.user, db.password, {
  host: db.host,
  dialect: 'mysql',
  operatorsAliases: false,
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  }
});

sequelize
  .authenticate()
  .then(() => {
    //Import Routes
    require('./app/routes/')(router, sequelize);

    router.get('/', (req, res) => {
      res.json('Welcome to Dickson Connect API :)');
    })

    //Make express Listen
    app.listen(PORT, () => {
      console.log('We are live on ' + PORT);
    })

  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });

//For chai testing
module.exports = app;

服务器正在工作。

和 test.js :

const chai = require('chai');
const chaitHttp = require('chai-http');
const server = require('../../server');

const should = chai.should();

chai.use(chaitHttp);

describe('/GET', () => {

  it('should display a welcome message', (done) => {
    chai.request(server)
    .get('/')
    .then( (res) => {

      res.should.have.status(200);

      done();
    })
    .catch( err => {
      throw err;
    })
  })
})

我相信至少部分问题是我的服务器正在返回一个包含 express 应用程序的 sequelize 实例,这可能不是通常的情况。虽然,续集只是我在我的 chai 测试中等待的一个承诺,使用then而不是end.

这是我得到的错误:

/GET (node:35436) UnhandledPromiseRejectionWarning: AssertionError: expected { Object (domain, _events, ...) } 的状态码为 200,但在 chai.request.get.then (/Applications/MAMP/htdocs/api_dickson/) 获得 404 app/routes/index.test.js:16:23) at process._tickCallback (internal/process/next_tick.js:188:7) (node:35436) UnhandledPromiseRejectionWarning: 未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。(拒绝 id:1)(节点:35436)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。执行(默认):

0 次通过(2 秒) 1 次失败

1) /GET 应该显示欢迎消息:错误:超过 2000 毫秒的超时。对于异步测试和钩子,确保调用了“done()”;如果返回 Promise,请确保它已解决。

不需要告诉你我是从那些测试的东西开始的(最后......),因此,我还没有得到所有东西。非常感谢你的帮助 !

帕姆

4

1 回答 1

1

UnhandledPromiseRejectionWarning你来自你的测试,尝试在断言块之后做,.then(done, done)而不是调用done()和添加一个.catch块。

it('should display a welcome message', (done) => {
  chai.request(server).get('/')
  .then((res) => {
    res.should.have.status(200);
  })
  .then(done, done);
})

另外,关于 404,这是因为您在 Promise 中设置了路由sequelize.authenticate(),所以当您导出应用程序进行测试时,不会设置路由。只需将路由定义(并添加一条app.use('/', router);语句,否则您的路由将不会被使用)移动到 Promise 之上。

(...)
const sequelize = new Sequelize(...);

require('./app/routes/')(router, sequelize);
router.get('/', (req, res) => {
  res.json('Welcome to Dickson Connect API :)');
})

app.use("/", router);

sequelize
.authenticate()
.then(() => {
  //Make express Listen
  app.listen(PORT, () => {
    console.log('We are live on ' + PORT);
  })
})
.catch(err => {
  console.error('Unable to connect to the database:', err);
});

//For chai testing
module.exports = app;
于 2018-04-06T07:57:51.073 回答