简体   繁体   English

如何让附加到全局的属性与 sinon 一起正确存根?

[英]How can I get a property attached to a global to stub properly with sinon?

I have a helper.js that loads before my tests:我有一个helper.js在我的测试之前加载:

before(async function() {
    this.timeout(30000)
    global.db = require(`${process.cwd()}/models`)()
    ...

Then in my test, I have:然后在我的测试中,我有:

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({})
    })

The twilioClient.calls.create stubs properly. twilioClient.calls.create存根正确。 But the global.db.models.Call.update does not.但是global.db.models.Call.update没有。

In my actual code, I'm using it like:在我的实际代码中,我像这样使用它:

        await global.db.models.Call.update({ status: newCallStatus }, { where: { id: foundConversation.Call.id } })

When I console.log(global.db.models.Call) , it simply outputs Call .当我console.log(global.db.models.Call)时,它只是输出Call However, the .update function is there and does what it's supposed to do (a Sequelize model that updates).但是, .update function 在那里并且做了它应该做的事情(更新的 Sequelize model)。

I'm sure It's something terribly obvious, so any pointers would be greatly appreciated.我敢肯定这是非常明显的事情,所以任何指针都将不胜感激。 Thanks.谢谢。

The sequelize model methods are defined as prototype by the sequelize core. sequelize model 方法由 sequelize 核心定义为prototype

The following should work以下应该工作

updateCallSpy = sinon.stub(global.db.models.Call.prototype, 'update').resolves(true)

You can also create a stub instance:您还可以创建存根实例:

updateCallStubInstance = sinon.createStubInstance(global.db.models.Call)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM