我希望测试一些写入mongodb的业务逻辑。写完后。我希望查询数据库以断言该操作已正确完成。我无法在测试中定义模型并对此进行查询。
jest.config.js
{
testEnvironment: 'node',
}
最佳测试文件
const mongoose = require('mongoose');
describe('mongoose', () => {
beforeAll(async () => {
await mongoose.connect('mongodb://localhost:27017/test_database');
console.log('connected!');
});
afterAll(async () => {
await mongoose.disconnect();
console.log('disconnected!');
});
it('should find records in the database', async () => {
const model = mongoose.Schema({
key: mongoose.Schema.Types.String,
});
const Record = mongoose.model('record', model);
// ^ ERROR
const result = await Record.find({});
expect(result).toBeDefined();
});
});
尽管使用模型时出现错误:
TypeError: Cannot read property 'Decimal128' of null
at Object.<anonymous> (node_modules/mongoose/lib/types/decimal128.js:13:44)
at Object.<anonymous> (node_modules/mongoose/lib/utils.js:11:17)
at Object.<anonymous> (node_modules/mongoose/lib/statemachine.js:8:15)
at Object.<anonymous> (node_modules/mongoose/lib/internal.js:7:22)
关于我需要做什么的任何建议?
更新
我已经通过Mocha进行了这项工作。 :thinking:甚至可以开玩笑地做这种事情吗?在我看来,测试跑步者就是测试跑步者。仍然对您的答案感兴趣。