我如何获得连接到全局的属性以使用sinon正确地存根?

问题描述 投票:0回答:1

我有一个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模型)。

我确定这是非常明显的,因此任何指针将不胜感激。谢谢。

javascript mocha sequelize.js sinon stub
1个回答
1
投票

sequelize模型方法由sequelize核心定义为prototype

以下内容应该起作用

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

您也可以创建一个存根实例:

updateCallStubInstance = sinon.createStubInstance(global.db.models.Call)
© www.soinside.com 2019 - 2024. All rights reserved.