使用带 MySQL 的 Bookshelf 测试项目时随机超时
Random timeout when testing project using Bookshelf with MySQL
我第一次 运行 我的测试规范所有测试都通过了,但是如果我继续 运行 它再次导致测试超时,检查是否返回错误外键值无效。
这是我的代码错误还是与 Bookshelf 甚至 Mocha 相关的已知问题?
这是我的测试规范:
var _ = require('lodash'),
ModelsTaskList = require('../../models/taskList'),
TaskList = ModelsTaskList.TaskList,
server = require('../../index'),
chai = require('chai'),
chaiHttp = require('chai-http'),
should = chai.should(),
faker = require('faker');
chai.use(chaiHttp);
describe('TaskLists controller', function() {
describe('/POST tasklists', () => {
it('should not POST a tasklist without a status', (done) => {
var wrong_tasklist = _.clone(test_tasklist);
wrong_tasklist.statusId = undefined;
chai.request(server)
.post('/api/tasklists')
.send(wrong_tasklist)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('error');
done();
});
});
it('should not POST a tasklist with an invalid status', (done) => {
var wrong_tasklist = _.clone(test_tasklist);
wrong_tasklist.statusId = 99;
chai.request(server)
.post('/api/tasklists')
.send(wrong_tasklist)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('error');
done();
});
});
it('should not POST a tasklist without a user ID', (done) => {
var wrong_tasklist = _.clone(test_tasklist);
wrong_tasklist.createdBy = undefined;
chai.request(server)
.post('/api/tasklists')
.send(wrong_tasklist)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('error');
done();
});
});
it('should not POST a tasklist with an invalid user ID', (done) => {
var wrong_tasklist = _.clone(test_tasklist);
wrong_tasklist.createdBy = 99;
chai.request(server)
.post('/api/tasklists')
.send(wrong_tasklist)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('error');
done();
});
});
it('should not POST a tasklist with an estimated end date before the start date', (done) => {
var wrong_tasklist = _.clone(test_tasklist);
wrong_tasklist.startDate = faker.date.recent(1);
wrong_tasklist.estimatedEndDate = faker.date.recent(2);
chai.request(server)
.post('/api/tasklists')
.send(wrong_tasklist)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('error');
done();
});
});
})
});
我得到的第一次执行:
/POST 个任务列表
✓ 不应 POST 没有状态的任务列表 (49ms)
✓ 不应该 POST 一个状态无效的任务列表
✓ 不应 POST 没有用户 ID 的任务列表
✓ 不应 POST 具有无效用户 ID 的任务列表
✓ 不应 POST 任务列表的估计结束日期早于开始日期
接下来的处决:
/POST tasklists
✓ should not POST a tasklist without a status
1) should not POST a tasklist with an invalid status
✓ should not POST a tasklist without a user ID
2) should not POST a tasklist with an invalid user ID
✓ should not POST a tasklist with an estimated end date before the start date
4 passing (8s)
2 failing
1) TaskLists controller /POST tasklists should not POST a tasklist with an invalid status:
Error: Timeout of 4000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
2) TaskLists controller /POST tasklists should not POST a tasklist with an invalid user ID:
Error: Timeout of 4000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
下面是 POST 到 /api/tasklists 时执行的代码:
var ModelsTaskList = require('../models/taskList'),
TaskList = ModelsTaskList.TaskList,
moment = require('moment'),
ctlr;
var handleError = function (res, message) {
res.json({
status: false,
tasklist: null,
error: message
});
};
ctlr = {
create: function (req, res) {
if (req.body.statusId === undefined) {
handleError(res, 'ID do status não informado');
}
else if (req.body.createdBy === undefined) {
handleError(res, 'ID do usuário não informado');
}
else if (req.body.startDate !== undefined) {
var startDate = moment(req.body.startDate);
if (req.body.estimatedEndDate !== undefined) {
if (moment(req.body.estimatedEndDate).isBefore(startDate)) {
handleError(res, 'Data estimada de conclusão deve ser maior que a data de início');
}
}
}
else {
TaskList.forge(req.body)
.save({ method: 'insert', require: true })
.then(result => {
res.json({
status: true,
tasklist: result,
error: null
});
})
.catch(err => {
handleError(res, err.message);
});
}
},
read: function (req, res) {
}
};
module.exports = ctlr;
这是我的代码中的一个错误。根据数据库条件和正在发送的数据,以下 else if
语句未发送任何响应,导致超时:
else if (req.body.startDate !== undefined) {
var startDate = moment(req.body.startDate);
if (req.body.estimatedEndDate !== undefined) {
if (moment(req.body.estimatedEndDate).isBefore(startDate)) {
handleError(res, 'Data estimada de conclusão deve ser maior que a data de início');
}
}
// I MISSED AN ELSE HERE
}
是我的错。
我第一次 运行 我的测试规范所有测试都通过了,但是如果我继续 运行 它再次导致测试超时,检查是否返回错误外键值无效。
这是我的代码错误还是与 Bookshelf 甚至 Mocha 相关的已知问题?
这是我的测试规范:
var _ = require('lodash'),
ModelsTaskList = require('../../models/taskList'),
TaskList = ModelsTaskList.TaskList,
server = require('../../index'),
chai = require('chai'),
chaiHttp = require('chai-http'),
should = chai.should(),
faker = require('faker');
chai.use(chaiHttp);
describe('TaskLists controller', function() {
describe('/POST tasklists', () => {
it('should not POST a tasklist without a status', (done) => {
var wrong_tasklist = _.clone(test_tasklist);
wrong_tasklist.statusId = undefined;
chai.request(server)
.post('/api/tasklists')
.send(wrong_tasklist)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('error');
done();
});
});
it('should not POST a tasklist with an invalid status', (done) => {
var wrong_tasklist = _.clone(test_tasklist);
wrong_tasklist.statusId = 99;
chai.request(server)
.post('/api/tasklists')
.send(wrong_tasklist)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('error');
done();
});
});
it('should not POST a tasklist without a user ID', (done) => {
var wrong_tasklist = _.clone(test_tasklist);
wrong_tasklist.createdBy = undefined;
chai.request(server)
.post('/api/tasklists')
.send(wrong_tasklist)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('error');
done();
});
});
it('should not POST a tasklist with an invalid user ID', (done) => {
var wrong_tasklist = _.clone(test_tasklist);
wrong_tasklist.createdBy = 99;
chai.request(server)
.post('/api/tasklists')
.send(wrong_tasklist)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('error');
done();
});
});
it('should not POST a tasklist with an estimated end date before the start date', (done) => {
var wrong_tasklist = _.clone(test_tasklist);
wrong_tasklist.startDate = faker.date.recent(1);
wrong_tasklist.estimatedEndDate = faker.date.recent(2);
chai.request(server)
.post('/api/tasklists')
.send(wrong_tasklist)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.should.have.property('error');
done();
});
});
})
});
我得到的第一次执行:
/POST 个任务列表 ✓ 不应 POST 没有状态的任务列表 (49ms) ✓ 不应该 POST 一个状态无效的任务列表 ✓ 不应 POST 没有用户 ID 的任务列表 ✓ 不应 POST 具有无效用户 ID 的任务列表 ✓ 不应 POST 任务列表的估计结束日期早于开始日期
接下来的处决:
/POST tasklists
✓ should not POST a tasklist without a status
1) should not POST a tasklist with an invalid status
✓ should not POST a tasklist without a user ID
2) should not POST a tasklist with an invalid user ID
✓ should not POST a tasklist with an estimated end date before the start date
4 passing (8s)
2 failing
1) TaskLists controller /POST tasklists should not POST a tasklist with an invalid status:
Error: Timeout of 4000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
2) TaskLists controller /POST tasklists should not POST a tasklist with an invalid user ID:
Error: Timeout of 4000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
下面是 POST 到 /api/tasklists 时执行的代码:
var ModelsTaskList = require('../models/taskList'),
TaskList = ModelsTaskList.TaskList,
moment = require('moment'),
ctlr;
var handleError = function (res, message) {
res.json({
status: false,
tasklist: null,
error: message
});
};
ctlr = {
create: function (req, res) {
if (req.body.statusId === undefined) {
handleError(res, 'ID do status não informado');
}
else if (req.body.createdBy === undefined) {
handleError(res, 'ID do usuário não informado');
}
else if (req.body.startDate !== undefined) {
var startDate = moment(req.body.startDate);
if (req.body.estimatedEndDate !== undefined) {
if (moment(req.body.estimatedEndDate).isBefore(startDate)) {
handleError(res, 'Data estimada de conclusão deve ser maior que a data de início');
}
}
}
else {
TaskList.forge(req.body)
.save({ method: 'insert', require: true })
.then(result => {
res.json({
status: true,
tasklist: result,
error: null
});
})
.catch(err => {
handleError(res, err.message);
});
}
},
read: function (req, res) {
}
};
module.exports = ctlr;
这是我的代码中的一个错误。根据数据库条件和正在发送的数据,以下 else if
语句未发送任何响应,导致超时:
else if (req.body.startDate !== undefined) {
var startDate = moment(req.body.startDate);
if (req.body.estimatedEndDate !== undefined) {
if (moment(req.body.estimatedEndDate).isBefore(startDate)) {
handleError(res, 'Data estimada de conclusão deve ser maior que a data de início');
}
}
// I MISSED AN ELSE HERE
}
是我的错。