最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stubbing an ES6 class method using Mocha and Sinon in NodeJS - Stack Overflow

programmeradmin2浏览0评论

Is there a way to stub an ES6 class method using Mocha/Sinon?

I'm trying to do this...

sinon.stub(Factory, 'announce');

but I just get the following error...

TypeError: Attempted to wrap undefined property announce as function

Is there a way to stub an ES6 class method using Mocha/Sinon?

I'm trying to do this...

sinon.stub(Factory, 'announce');

but I just get the following error...

TypeError: Attempted to wrap undefined property announce as function
Share Improve this question asked May 26, 2016 at 21:47 michaelmichael 4,4836 gold badges41 silver badges57 bronze badges 2
  • 1 Shouldn't you pass Factory.prototype? I guess it's not a static method. – Bergi Commented May 27, 2016 at 0:01
  • 1 I didn't realise that ES6 classes still used .prototype! Want to post an answer for me to accept? – michael Commented May 27, 2016 at 12:02
Add a comment  | 

1 Answer 1

Reset to default 22

Instance methods are still placed on the prototype object of a class to be inherited from, not on its constructor, even if the class syntax obscures that a bit. Use

sinon.stub(Factory.prototype, 'announce');
发布评论

评论列表(0)

  1. 暂无评论