关于javascript:使用chai-http对express + sequelize服务器执行Ping操作

Ping an express + sequelize server with chai-http

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

server.js代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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应用程序的续集实例,这可能不是通常的情况。 不过,续集只是我在使用chai测试时正在等待的承诺,使用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.

无需告诉您,我是从那些测试材料开始的(最终...),因此,我还没有掌握所有信息。 非常感谢你的帮助 !

PAM


您拥有的UnhandledPromiseRejectionWarning来自测试,请尝试在断言块之后执行.then(done, done),而不是调用done()并添加.catch块。

1
2
3
4
5
6
7
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中设置了路由,因此当导出应用程序进行测试时,不会设置路由。 只需在Promise上方移动路线定义(并添加app.use('/', router);语句,否则将不会使用您的路线)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(...)
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;