我在尝试建立数据库以进行测试时遇到了一些麻烦。应该删除存储在数据库中的数据,并为每个测试重新填充。我目前正在执行以下操作:
db.js
const mongoose = require('mongoose');
// a Mongoose model describing an entity
const Entity = require('entity-model');
// entities.mock is an array containing entity objects.
const mockedEntities= require('./entities.mock');
function setUp() {
Entities.collection.insertMany(mockedEntities);
}
function breakDown() {
mongoose.connection.on('connected',() => {
mongoose.connection.db.dropDatabase();
});
}
module.exports = { setUp,breakDown };
然后在我的 test.js 中:
const db = require('./db');
describe('e2e tests to make sure all endpoints return the correct data from the database',() => {
beforeEach(async () => {
await db.breakDown();
db.setUp();
});
it('should check store-test-result (UR-101)',(done) => ...perform test);
it('should check store-nirs-device (UR-102)',(done) => ...perform test);
});
在正确重新填充数据库之前,似乎没有清空数据库。关于可能的原因有什么建议吗?