我用的是猫鼬2.13.2和猫鼬5.12.2,还有打字稿和笑话。在我的测试中,我试图模拟对我的模式的find方法的调用,所以我尝试了下面的方法
import mockingoose from 'mockingoose';
...
beforeEach(async () => {
jest.resetAllMocks();
jest.clearAllMocks();
mockingoose(File).reset();
console.log("mock response:" + JSON.stringify(fileMockResponse));
mockingoose(File).toReturn(fileMockResponse, 'find');
const filePostList = await File.find({
_id: { $in: ['test'] },
});
console.log("mocking file post list:" + JSON.stringify(filePostList));但是,当测试执行时,将记录以下内容:
mock response:[{"data:" ... "}]
at Suite.<anonymous> (routes/file.test.ts:237:15)
console.log
mocking file post list:undefinedundefined告诉我,我试图模拟对find调用的响应失败。
我的模型/模式如下所示:
export interface IFile extends Document {
author: string;
...
}
const FileSchema: Schema = new Schema(
{
author: { type: String, required: false },
...
}
export default mongoose.model<IFile>('File', FileSchema);发布于 2022-04-13 21:05:11
您试过在架构之外实例化模型吗?只导出模式,然后在导入模式的外部(在测试用例中)实例化模型。
当我遇到类似的问题时,我就是这样做的。
https://stackoverflow.com/questions/68025008
复制相似问题