• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

替换类的javascript方法进行测试?

用户头像
it1352
帮助1

问题说明

我想测试一个调用其超级方法foo"的方法foo":

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
}

...我可以简单地替换 baa 方法进行测试,例如

... 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).

但是,如何替换超级方法'foo'?

However, how to replace the super method 'foo'?

也许类似

element.constructor.getSuperClass().foo = () => { console.log('This is a mock of super foo')};

但是,我还没有获得超级课程.

However, I did not manage to get the super class, yet.

相关问题:

监视/模拟 es6 中的超类方法

正确答案

#1
element.constructor.__proto__.prototype.foo = () => {
                        console.log('This is a mock of super foo');
                    };

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfgabg
系列文章
更多 icon
同类精品
更多 icon
继续加载