gpt4 book ai didi

javascript - 如何在 Node.js 中模拟 sequelize 模型方法

转载 作者:行者123 更新时间:2023-12-05 00:24:55 25 4
gpt4 key购买 nike

在编写junit时,我一直在努力在我的一个副项目中模拟数据库。有人可以帮我吗?下面是场景的样子:

在此之前,源代码在这里 - https://github.com/sunilkumarc/track-courier

  • 我在 Nodejs 中使用 sequelize 模块创建了一个模型。我通过这个模型访问我的数据库。
  • 我想在运行 junit 时模拟 db 调用。例如 findOne 方法在这里返回一个 promise ( https://github.com/sunilkumarc/track-courier/blob/master/models/parcels.js#L4 )。基本上在运行这个特定端点时,我想跳过访问数据库。

  • 任何帮助表示赞赏!

    问候, 苏尼尔

    最佳答案

    我曾经使用 Sinon.JS 来模拟任何东西,在这种情况下,我是这样做的:

    // assume "Users" is our table

    sinon.replace(Users, "create", () => console.log(`mocked "Users" table's "create" method`));

    关于javascript - 如何在 Node.js 中模拟 sequelize 模型方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42398728/

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