平安快递 + sequelize server with chai-http
Ping an express + sequelize server with 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 测试中等待的承诺,使用 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
块。
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;
我在使用 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 测试中等待的承诺,使用 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
块。
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;