我有一个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模型方法由sequelize核心定义为prototype
。
以下内容应该起作用
updateCallSpy = sinon.stub(global.db.models.Call.prototype, 'update').resolves(true)
您也可以创建一个存根实例:
updateCallStubInstance = sinon.createStubInstance(global.db.models.Call)