Skip to content
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

Documentation for Getter/Setter spying and stubbing #1606

Closed
loganhuskins opened this issue Nov 2, 2017 · 10 comments · Fixed by #1976
Closed

Documentation for Getter/Setter spying and stubbing #1606

loganhuskins opened this issue Nov 2, 2017 · 10 comments · Fixed by #1976

Comments

@loganhuskins
Copy link

Hey all,

Is there currently documentation anywhere on how to use the getter and setter spying features outlined in #1205? If not, I could read through the code and tests and add that in if it's needed. I searched pretty hard and did not see it anywhere do I figured I'd ask. Thanks.

@fatso83
Copy link
Contributor

fatso83 commented Nov 2, 2017

Is this what you were looking for?

I also see this written, but it seems outdated (used to be true):

If you need to stub getters/setters or non-function properties, then you should be using sandbox.stub

Normal stubs can be used for that now.

@loganhuskins
Copy link
Author

For stubs that looks like it. I don't know of there is anything in the spy docs for them.

@fatso83
Copy link
Contributor

fatso83 commented Nov 2, 2017

Hmm ... I don't actually think spying has been implemented for getters/setters. I don't really know why either. The implementation would basically just be something like this:

fn=Object.getOwnPropertyDescriptor(obj, prop).get
spy=sinon.spy(fn);
Object.defineProperty(obj, prop, { get: spy })

Care to take a stab at it?

@loganhuskins
Copy link
Author

loganhuskins commented Nov 2, 2017

It has been implemented and I am using it. I found out how by looking through the tests in #1205. I found the tests here.

It is done like this:

spy = sinon.spy(className, 'nameOfGetterFunction', ['get'])

Should this be in the docs? Or is that implementation not even a known thing at this point?

@mroderick
Copy link
Member

It would be great to have it documented, thank you!

@loganhuskins
Copy link
Author

For sure, I'll knock it out.

@stale
Copy link

stale bot commented Jan 13, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@mouse9
Copy link

mouse9 commented Jun 22, 2018

Is there any way to spy it without class ( Directly by object )?
I do have only object there in the spec and couldn't require the class of the getter setter is there any other way also?

@fatso83
Copy link
Contributor

fatso83 commented Jun 22, 2018

@mouse9 It is unclear what you are asking, and how that relates to stubbing/spying on getters. I don't understand what you mean by classes either. Classes has no real meaning in javascript and in most regards what you think of as classes are nothing more than objects.

In any case, we are trying to keep the GitHub issues list tidy and focused on bugs and feature discussions. This ticket looks like a usage question; please post it to StackOverflow and tag it with sinon, so the bigger community can help answer your questions.

If you feel that your topic is an issue with Sinon, please open a new ticket and follow the guidelines for reporting an issue.

@seanyang414
Copy link

For @loganhuskins 's code:
spy = sinon.spy(className, 'nameOfGetterFunction', ['get'])

Found that, in assertion, do need to assert on spy.get like:
sinon.assert.calledOnce(spy.get);

Previously was asserted on spy only, and didn't work.

@fatso83 fatso83 added the Property accessors Property Getters/Setters label Jun 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants