我有一些使用sinon存根的問題,它可能來自我如何在模塊上實現命名空間,我期待存根。直接在原型上定義的方法按我所期望的那樣存在。sinon stub命名空間函數
...my module.js
const Constructor = require('./constructor') //...just exports a singleton
/* Need to namespace some of my functions and retain the `this` context */
Object.defineProperty(Constructor.prototype, 'es', {
get: function() {
return {
method: require('./implementations/doesSomething.js').bind(this)
}
}
});
module.exports = Constructor;
/* ...testFile.js */
const Constructor = require('./constructor');
const instance = new Constructor();
const sinon = require('sinon');
sinon.stub(instance.es, 'method', function() {
return 'hijacked original method'
});
什麼錯誤是你越來越? – Arcath
@Arcath我沒有收到任何錯誤,它根本就不存在任何方法。我也注意到,如果我把console.log檢查它是否確實是一個sinon存根,而不是。 –