Используя sinon, как мне заглушить / подделать возвращаемое значение this.lastID в функции db.run.
module.exports.insert = async(request) => {
//unimportant code
return new Promise((resolve, reject) => {
db.run(sql, params, function(err) {
if (err)
reject(err)
else
resolve(this.lastID)
})
})
}
Я могу подделать обратный вызов, используя этот код:
describe('insert', () => {
beforeEach(() => {
this.insert = sinon.stub(db, 'run')
.callsArgWith(2, null)
})
afterEach(() => {
this.insert.restore()
})
test('add product to the database', async(done) => {
expect.assertions(1)
const id = await productDb.insert(testProductAlt)
expect(isNaN(id)).toBe(false)
expect(id).toBe('1')
done()
})
})
Но произойдет сбой, так как this.lastID не определен.Как мне преодолеть это?
Спасибо!