-
-
Notifications
You must be signed in to change notification settings - Fork 769
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
Stubbing a property getter works, but stub.called remains false #1545
Comments
This seems to have been like that forever. Not sure if this is a bug or not, as you are not actually calling the stub, but the getter set through the stub. And there are further complications, as you might see from this: sinon.stub(myObj, 'prop').get(getterFunction).set(setterFunction);
myObj.prop = 1;
myObj.prop;
myObj.prop = 2; The stub now has two methods. How would we go about this when trying to count |
I'll close this as a non-bug, but we could perhaps need to clarify this in the docs. Feel free to update them. |
How would I mock/stub the value of the property, and check it was called? |
I am not sure if a "best" way, but one way (although inelegant) is to make the getter a stub too, and check on that. |
Hi, may I have some guidance on how to make this getter a stub?
How do I stub the getter of |
@yongish We are trying to keep the issue tracker focused on actual bugs. If you need help with something I suggest you try posting to StackOverflow (remember the |
For those trying to figure out how to do this, this thread hints at the solution: const obj = {
get a() { },
set a() { },
};
const getterSpy = sinon.spy();
const setterSpy = sinon.spy();
sinon.stub(obj, 'a')
.get(getterSpy)
.set(setterSpy);
getterSpy.calledOnce; // false
obj.a;
getterSpy.calledOnce; // true
setterSpy.calledOnce; // false
obj.a = 'foo';
setterSpy.calledOnce; // true I haven't tried spying on both the getter and setter at the same time, but I know the above works individually. |
That's interesting. Presumably you can use (Thinking aloud...) In fact, if you were to write |
Yes, if you needed to actually return something, that does work: const sandbox = createSandbox();
const ss = {};
ss.location = sandbox.stub(window, 'location').value({ search: '?' });
ss.search = sandbox.stub(window.location, 'search').value('?foo=bar');
window.location.search; // ?foo=bar
ss.search.value('?qux=zed');
window.location.search; // ?qux=zed I couldn't get it to work by passing the sub-stub directly to the ss.search = sandbox.stub().value('?foo=bar'); // error
ss.location = sandbox.stub(window, 'location').value({ search: ss.search }); The anon triggers an undefinable error (despite the docs saying anons are supported).
@RoystonS, you would get an error that obj.a is not a function. If it didn't throw, the spy would be useless because Or, if you wrapped it in the sinon.spy after setting up the stub+spy, that would probably get an error about 'a' being undefined (because obj.a is now a proxy). |
I think this might be officially documented now. Check the section named "Using a spy to wrap property getter and setter" EDIT: I experimented with @JakobJingleheimer 's example and compared it to the official one, learning a lot in the process. This StackOverflow post goes into more detail.
Why doesn't this work when you give the stub a name and use it in your assertions instead? i.e.,: ...
const stubProperty = sinon.stub(obj, 'a')
.get(getterSpy)
.set(setterSpy);
stubProperty.calledOnce; // false
obj.a;
stubProperty.calledOnce; // always false
stubProperty.calledOnce; // false
obj.a = 'foo';
stubProperty.calledOnce; // always false It would make more sense to me if |
Um, it's been 6 years. I presume the problem is how you're accessing it (indirectly instead of the spy/stub itself). |
Sorry, I didn't communicate this well, but I was throwing that out into the ether for anyone to answer.
I think so too. Maybe I'm mistaken, but with stubbed functions/methods, I don't recall this posing a problem; it feels like stubbing properties makes the sinon API work differently. Either way, I experimented with your example and learned a lot in the process. This StackOverflow post goes into more detail. |
const getterSpy = sinon.spy();
const setterSpy = sinon.spy();
const obj = {
a: 42,
};
const stubProperty = sinon.stub(obj, "a").get(getterSpy).set(setterSpy);
const b = obj.a;
const c = obj.a;
obj.a = 43;
console.log(getterSpy.callCount); // 2
console.log(setterSpy.callCount); // 1 |
Stubbing a property getter works, but
stub.called
remainsfalse
.What did you expect to happen?
Getting a stubbed property should set
stub.called
totrue
.What actually happens
stub.called
remainsfalse
How to reproduce
The text was updated successfully, but these errors were encountered: