Here's the code I'm working on:
Test.js
class Test {
constructor() {
}
func () {
console.log('original')
}
}
module.exports = Test
Mock.js
Test = require('./Test')
function Mock(){
this.test = new Test()
}
Mock.prototype.call = function() {
this.test.func()
}
module.exports = Mock
I'm trying to stub the Test.func
call that's inside Mock.call
. I've tried this:
sb = sinon.createSandbox()
sb.stub(Test, 'func').callsFake(() => { console.log('stubbed') })
But I get TypeError: Cannot stub non-existent own property func
. When I do this:
sb.stub(new Test(), 'func').callsFake(() => { console.log('stubbed') })
new Mock().call()
I get original
printed, meaning that the stub did not work properly. How do I stub out the function call?
You need to stub it on instance which you create:
var mock = new Mock();
sinon.stub(mock.test, 'func').callsFake(() => { console.log('stubbed') });
mock.call(); // should output "stubbed"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.