Sinon: Documentation for Getter/Setter spying and stubbing

Created on 2 Nov 2017  ·  10Comments  ·  Source: sinonjs/sinon

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.

Documentation Help wanted Property accessors pinned

Most helpful comment

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?

All 10 comments

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.

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

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?

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?

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

For sure, I'll knock it out.

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.

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?

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

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.

Was this page helpful?
0 / 5 - 0 ratings