【问题标题】:Ping an express + sequelize server with chai-http使用 chai-http ping 一个 express + sequelize 服务器
【发布时间】:2018-09-15 11:22:14
【问题描述】:

我在使用 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

这是我得到的错误:

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

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

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

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

PAM

【问题讨论】:

  • 在这一行:require('./app/routes/')(router, sequelize);续集的目的是什么?
  • 嗨。此行的目的是将数据库连接(续集)传递给其他路由。

标签: javascript node.js express sequelize.js chai-http


【解决方案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,这是因为您在 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 2021-02-07
    • 2017-03-18
    • 2011-10-22
    • 2017-09-06
    相关资源
    最近更新 更多