如何模拟像findOne()这样的mongoose查询?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何模拟像findOne()这样的mongoose查询?相关的知识,希望对你有一定的参考价值。

我要模仿mongoose函数qazxsw poi。这就是我尝试过的。

1)

find()

2)

jest.mock("./user.model")
UserModel.findOne.mockResolvedValue(await UserModel.findOne({email: "test@gmail.com"}))

但两者都不起作用,什么是解决方案?我想让UserModel的findOne始终返回特定记录。

谢谢

答案

假设const findOne = jest.fn(); findOne.mockResolvedValue(await UserModel.findOne({email: "test@gmail.com"})) UserModel模型实例,你可以做以下事情:

mongoose

一些是参考:

  • jest.spyOn(UserModel, 'findOne').mockReturnValue(Promise.resolve({ email: "test@gmail.com" }))
  • mockReturnValue

以上是关于如何模拟像findOne()这样的mongoose查询?的主要内容,如果未能解决你的问题,请参考以下文章