【发布时间】:2021-08-18 14:50:07
【问题描述】:
我正在尝试模拟 MongoClient 的 insertMany 函数。有了这个模拟,我不会使用真正的 mongo db,而是使用 Jest 的模拟实现。但是我遇到了类似的错误
DbConnection.db(...).collection 不是函数
await DbConnection.db().collection(this.collectionName).insertMany(logs);
模拟
const DbConnection = {
db: () => jest.fn().mockImplementationOnce(() =>({
collection: () => jest.fn().mockImplementationOnce(() =>({
insertMany: () => {
return { success: true }
},
})),
})),
close: async () => true
};
错误
DbConnection.db(...).collection is not a function
【问题讨论】:
标签: javascript mongodb unit-testing jestjs mocking