javascript - 平安 express + sequelize server with chai-http

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

我在使用 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 实例,这可能不是通常的情况。虽然,sequelize 只是我在 chai 测试中等待的 promise ,使用 then 而不是 end

这是我遇到的错误:

/GET (node:35436) UnhandledPromiseRejectionWarning: AssertionError: expected { Object (domain, _events, ...) } to have status code 200 but got 404 at chai.request.get.then (/Applications/MAMP/htdocs/api_dickson/app/routes/index.test.js:16:23) at at process._tickCallback (internal/process/next_tick.js:188:7) (node:35436) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:35436) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. Executing (default): SELECT 1+1 AS result We are live on 8000 1) should display a welcome message

0 passing (2s) 1 failing

1) /GET should display a welcome message: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

无需告诉您我正从那些测试 Material 开始(终于……),因此,我还没有得到所有东西。非常感谢您的帮助!

PAM

最佳答案

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

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

此外,关于 404,这是因为您在 sequelize.authenticate() promise 中设置了路由,因此当您导出应用程序进行测试时,没有设置路由。只需移动路由定义(并添加 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;

关于javascript - 平安 express + sequelize server with chai-http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49675711/

相关文章:

javascript - 我想使用 javascript 调用 Controller

javascript - 设置元素的值

javascript - module exports = mongoose 模型在 NodeJs 中做什么

javascript - ejs <%- body %> 不工作

javascript - 客户端无法访问自己的服务器端点

javascript - 组件未完全渲染且不随 State 的变化而变化

javascript - 如何让 HTML5 JavaScript Canvas 识别来自 iphone/ipad 的触摸/点击?

node.js - 使用node.js和mongoDB向map.jade文件(传单)添加标记

javascript - 隐式声明 'let' 对带递归的 for 循环变量有什么影响?

javascript - Babel 6 regeneratorRuntime 未定义