gpt4 book ai didi

javascript - 如何模拟模拟find()的toArray()?

转载 作者:行者123 更新时间:2023-11-29 20:55:36 26 4
gpt4 key购买 nike

对于这个方法

content.js

const content = await Content.findOne({ _id: articleId })

我进行模拟:

content.test.js

Content.findOne = jest.fn(() => Promise.resolve({ some: 'content' }))

但是如何模拟 mongo native 驱动程序使用的 find.toArray() 方法?

const posts = await Content.find({ category: 'foo' }).toArray()

最佳答案

由于您正在模拟 Content 的属性,我想说继续这样做。让 Content.find 返回一个具有 toArray 属性的对象,该属性是一个可调用函数:

Content.find = jest.fn(() => ({ toArray: _ => [
{ some: 'content' },
{ some: 'content' }
] }));

关于javascript - 如何模拟模拟find()的toArray()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49496751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com