New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make stub work for prototype methods #54
Conversation
This is incredibly useful if you work with named constructors and prototype methods.
+1 to that. We had this discussion over twitter: https://twitter.com/#!/cjno/status/126676122242007040 While the argument "I don't want Sinon to help you successfully test an object that uses a collaborator in a way that fails in production" is totally valid, I still think there should be a way to stub objects with prototype methods too. If not within the stub method then maybe in something like "stubWithPrototype" (bit verbose though). |
If we're going to allow stubbing inherited properties, is there any point in restricting it to the first object on the prototype chain? I.e. do we need the |
That would work for me. Generally if I do Let me know and I will modify the this patch. |
I think I agree. I'm not entirely sure why the hasOwnProperty check is in there. What I don't want Sinon to do is to fake non-existing properties (i.e. Pull out the filter, and I'll pull it! |
Btw. I just figured out that another rather doable way is: var stub = sinon.stub(Object.create(MyClass.prototype)); But generally I would probably still prefer what's discussed above as it feels more natural to do the stubbing that way. |
That's definitely less easy on the eyes :) But I'll pull your change if you pick out the filter, so it just stubs whatever method the object has, regardless of prototype chain origin. |
Turns out it wasn't actually working with an unpatched version of sinon either (npm was doing unexpected things, see https://github.com/isaacs/npm/issues/1727). Anyway, I submitted the discussed simplification above, all tests passing! |
Only stub functions, not properties. Added a test for this as there was none so far.
Sorry for all the little commits, I can squash this into one if you'd like me to. But should be good now. |
No, I like small commits :) Thanks again! |
Make stub work for prototype methods
Sweet, thanks a lot! |
This makes perfect sense! So much sense, in fact, that I'm confused spies don't do the same; shouldn't they share most of their API..? I'm on latest stable. Cheers! |
@jareware I guess that would make sense. I'll gladly merge it if you want to fix it. |
This is incredibly useful if you work with named constructors and
prototype methods.
All my classes look like this:
Without this patch
sinon.stub(new MyClass)
does not stub my prototype methods.Let me know what you think!