问题
I want to test a method 'foo' that calls its super method 'foo':
foo(){
super.foo();
//do some extra stuff
}
If it would reference another method from the same class, e.g.
foo(){
this.baa();
//do some extra stuff
}
... i could simply replace the baa method for testing, e.g
element.baa = () => { console.log('This is a mock of baa')};
element.foo();
expect(effectsOfFoo).toBe(Ok).
However, how to replace the super method 'foo'?
Maybe something like
element.constructor.getSuperClass().foo = () => { console.log('This is a mock of super foo')};
However, I did not manage to get the super class, yet.
Related questions:
spying on/mocking super class methods in es6
回答1:
element.constructor.__proto__.prototype.foo = () => {
console.log('This is a mock of super foo');
};
来源:https://stackoverflow.com/questions/58410532/how-to-replace-javascript-method-of-super-class-for-testing