How to replace javascript method of super class for testing?

我的未来我决定 提交于 2019-12-08 13:58:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!