如何让 Chai 处理多个数据库查询?
How do I make Chai handle multiple database queries?
我正在尝试了解 Express + Mocha/Chai 中的单元测试。
我正在尝试测试的 API 调用似乎在第一个异步函数超时后停止,请参阅下面的相关代码和控制台输出。
我不确定 API 本身或测试中是否遗漏了什么!
// controller.js
const { Alert } = require('../models');
exports.addAlert = async (req, res) => {
const username = req.body.uploader !== "" ? req.body.uploader : "anonymous"
const rowNumber = await Alert.count()
const title = req.body.title !== "" ? req.body.title : parseInt(rowNumber) + 1
const addedAlert = await Alert.findOrCreate({
where: {
coordinates: req.body.coordinates
},
defaults: {
username: username,
path: req.file.path,
title: title
}
})
addedAlert[1] ?
res.send(200).render('home', {
formMessage: `Added alert ${title}`
}) :
res.send(303).render('home', {
formMessage: 'Coordinates already taken.'
})
};
// test.js
let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('../../app');
let should = chai.should();
chai.use(chaiHttp);
describe('/POST alert', () => {
it('it should POST an alert', (done) => {
chai.request(server)
.post('/add-alert')
.send({ username: "foo", title: "bar", coordinates: "baz", path: "foo/bar/baz" })
.end((err, res) => {
console.log(res.body)
done();
});
});
});
//mocha tests/**/*.* --exit
/POST alert
Executing (default): SELECT count(*) AS `count` FROM `Alerts` AS `Alert`;
1) it should POST an alert
/*
** Other tests that are working fine
*/
1 failing
1) /POST alert
it should POST an alert:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/*..*/test.js)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
我解决了我自己的问题:Mocha 没有继续,因为我没有将我用 Multer 上传的文件传递给它。
这有效:
it('it should POST an alert', async function() {
await request(server)
.post('/add-alert')
.set('Content-Type', 'multipart/form-data')
.field('uploader', 'foo')
.field('coordinates', 'bar')
.field('title', 'baz')
.attach('image',
fs.readFileSync('./dist/images/image-1617995909552.jpg'),
'image-1617995909552.jpg')
.expect(200)
});
我正在尝试了解 Express + Mocha/Chai 中的单元测试。
我正在尝试测试的 API 调用似乎在第一个异步函数超时后停止,请参阅下面的相关代码和控制台输出。
我不确定 API 本身或测试中是否遗漏了什么!
// controller.js
const { Alert } = require('../models');
exports.addAlert = async (req, res) => {
const username = req.body.uploader !== "" ? req.body.uploader : "anonymous"
const rowNumber = await Alert.count()
const title = req.body.title !== "" ? req.body.title : parseInt(rowNumber) + 1
const addedAlert = await Alert.findOrCreate({
where: {
coordinates: req.body.coordinates
},
defaults: {
username: username,
path: req.file.path,
title: title
}
})
addedAlert[1] ?
res.send(200).render('home', {
formMessage: `Added alert ${title}`
}) :
res.send(303).render('home', {
formMessage: 'Coordinates already taken.'
})
};
// test.js
let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('../../app');
let should = chai.should();
chai.use(chaiHttp);
describe('/POST alert', () => {
it('it should POST an alert', (done) => {
chai.request(server)
.post('/add-alert')
.send({ username: "foo", title: "bar", coordinates: "baz", path: "foo/bar/baz" })
.end((err, res) => {
console.log(res.body)
done();
});
});
});
//mocha tests/**/*.* --exit
/POST alert
Executing (default): SELECT count(*) AS `count` FROM `Alerts` AS `Alert`;
1) it should POST an alert
/*
** Other tests that are working fine
*/
1 failing
1) /POST alert
it should POST an alert:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/*..*/test.js)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
我解决了我自己的问题:Mocha 没有继续,因为我没有将我用 Multer 上传的文件传递给它。
这有效:
it('it should POST an alert', async function() {
await request(server)
.post('/add-alert')
.set('Content-Type', 'multipart/form-data')
.field('uploader', 'foo')
.field('coordinates', 'bar')
.field('title', 'baz')
.attach('image',
fs.readFileSync('./dist/images/image-1617995909552.jpg'),
'image-1617995909552.jpg')
.expect(200)
});