javascript - 如何让附加到全局的属性与 sinon 一起正确存根?
问题描述
我有一个helper.js
在我的测试之前加载的:
before(async function() {
this.timeout(30000)
global.db = require(`${process.cwd()}/models`)()
...
然后在我的测试中,我有:
describe.only('Phone Library', () => {
let updateCallSpy
beforeEach(() => {
sinon.stub(twilioClient.calls, 'create').resolves({})
updateCallSpy = sinon.stub(global.db.models.Call, 'update').resolves(true)
// sinon.stub(global.db.models.Conversation, 'update').resolves({})
})
twilioClient.calls.create
存根正确。但是global.db.models.Call.update
没有。
在我的实际代码中,我像这样使用它:
await global.db.models.Call.update({ status: newCallStatus }, { where: { id: foundConversation.Call.id } })
当我console.log(global.db.models.Call)
,它只是输出Call
。但是,该.update
功能在那里并执行它应该做的事情(更新的 Sequelize 模型)。
我敢肯定这是非常明显的事情,所以任何指针都将不胜感激。谢谢。
解决方案
sequelize 模型方法prototype
由 sequelize 核心定义。
以下应该工作
updateCallSpy = sinon.stub(global.db.models.Call.prototype, 'update').resolves(true)
您还可以创建存根实例:
updateCallStubInstance = sinon.createStubInstance(global.db.models.Call)
推荐阅读
- python - 执行功能后 tkinter 锁定
- javascript - 在 Wordpress 的 wp-admin/post.php 页面中,我可以在浏览器控制台中输入什么来获取登录用户的显示名称?
- google-colaboratory - Colab - 使用 GDAL 拆分大 tiff 文件
- python - Matplotlib - 图表对象上的 plt.show()
- android - 根据 Volley 的响应代码将用户重定向到某个活动(Android)
- python-3.x - 使用 python/pandas 查找客户状态分类
- python - 使用列表比较和更新 CSV 文件
- java - 持续事件时抛出 akka.pattern.AskTimeoutException
- css - 无法在语义 UI 中操作背景高度
- mongodb - 有没有办法在 Heroku 上托管使用 MongoDB Atlas 的应用程序而无需支付插件费用?