第二次运行它时,它运行良好。仅当我同时运行它们时,我收到一条错误消息:“无法读取null的属性'driving'。”
在第一个测试结束之前,DELETE测试似乎正在接触数据库。如何防止这种情况发生?
describe('Drivers controller', () => {
it('PUT to /api/drivers/:id edits an existing driver', done => {
const driver = new Driver({ email: 't@t.com', driving: false });
driver.save()
.then(() => {
chai.request(app)
.put(`/api/drivers/${driver._id}`)
.send({ driving: true})
.end(() => {
Driver.findOne({ email: 't@t.com' })
.then(d => {
assert(d.driving === true);
});
});
})
.then(() => done());
});
it('DELETE request to /api/drivers/:id removes an existing user', done => {
const driver = new Driver({ email: 'test@test.com' });
driver.save().then(() => {
Driver.findOne({ email: 'test@test.com' })
.then(driver => assert(driver !== null))
.then(() => {
chai.request(app)
.delete(`/api/drivers/${driver._id}`)
.end(() => {
Driver.findOne({ email: 'test@test.com' }).then(driver => {
assert(driver === null);
done();
});
});
});
});
});
});