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

Fix spying on *etter methods #1205

Merged
merged 1 commit into from
Dec 18, 2016
Merged

Conversation

lucasfcosta
Copy link
Member

@lucasfcosta lucasfcosta commented Dec 15, 2016

Purpose (TL;DR)

This PR aims to fix #1018 on v1.17.
Basically I just needed to pass a new descriptor with only the property from the propDescriptor which is going to be spied on.
I also added tests to prove both stubbing and spying work as @mroderick did on the issue itself.

Background (Problem in detail)

The problem happened because the property descriptor being passed to be spied on had non-function property values (because it was a complete property descriptor taken from that property) and then that caused an error.

Solution

As I've said above I just needed to passa a property descriptor with only the property which was going to be spied instead of the whole property descriptor

How to verify

  1. Check out this branch (see github instructions below)
  2. npm install
  3. npm run test -> This will run the new tests I've added

Thanks for the awesome work you have been doing on Sinon, I'm looking forward to contribute more with you, please let me know if you need anything!

I've read the contribution guidelines but I'm not sure this PR meets your commit/coding patterns, please let me know if I've done anything wrong or missed anything and I'll happily fix it. You rock ❤️

EDIT: The build seems to be failing on CI for an unknown reason. I've read the log and I think it might be related to Travis itself and not this PR. Do you have any idea why this is happening?

Copy link
Contributor

@fatso83 fatso83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good as it is. The only thing I find a bit missing are code comments that tell why things are done. To the casual reader, there is very little help in seeing why the descriptor object is being introduced. I am assuming this is to avoid triggering the stubbed property? Could have been nice to see it commented.

On the other hand, this is just my opinion, and I am not sure the others agree. I just happen to like comments :-)

@fatso83
Copy link
Contributor

fatso83 commented Dec 17, 2016

Sorry for the slow followup ... you are right about the failing build. Restarting the Travis build seems to have fixed the failing tests.

@fatso83
Copy link
Contributor

fatso83 commented Dec 17, 2016

@mroderick objections to merging this? I could like a little comment or two, other than that ... 👍

@lucasfcosta
Copy link
Member Author

@fatso83 I made some comments while studying this code and doing the fix, but since the code didn't have any comments before I assumed those pieces of information would be considered unnecessary.
I'd be happy to add a few more comments to this code if you want me to, all you gotta do is let me know 😄

Thanks for the feedback!

@@ -2226,6 +2226,34 @@

assert.equals(spy.length, 3);
}
},

"etters": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the naming here. Either getters and setters or a getters section separate from the setters section.

Copy link
Member Author

@lucasfcosta lucasfcosta Dec 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! It really looks better, thanks for the feedback. I'll do it.

@fatso83
Copy link
Contributor

fatso83 commented Dec 17, 2016

@lucasfcosta, as long as comments document the intent, not the steps of the implementation, I think it usually helps the understanding of the reader. If you can add a small comment I think it would help future readers understanding the code. I also wasn't a fan of the "etters" naming, but it's not a big thing. Just change it per fearphage's comment and this should be good to merge.

@lucasfcosta
Copy link
Member Author

Here it is!

I made sure to add two concise comment lines explaining why we needed a new descriptor object whenever trying to spy on property descriptor's properties.

I also renamed both of those tests containing the getter and setter specific tests to getters and setters, it really looks better than *etters.

Thanks for the feedback @fatso83 and @fearphage, let me know if I missed anything.

@fatso83 fatso83 merged commit ba631c6 into sinonjs:v1.17 Dec 18, 2016
@fatso83
Copy link
Contributor

fatso83 commented Dec 18, 2016

Great work, and thanks for listening to the input. Much appreciated.

@muuki88
Copy link

muuki88 commented May 2, 2017

Hi,

Is there a migration path on how to implement this in the new 2.x.x version series? I see that the tests ignore the deprecation warnings.

Thanks a lot :)

@lucasfcosta
Copy link
Member Author

lucasfcosta commented May 3, 2017

Hi @muuki88, this is only a fix for the v1.17 version.
The implementation for this feature in v2 has happened at #1297.

These are the docs on how to implement getter and setters spies and stubs:

stub.get(getterFn)

Replaces a new getter for this stub.

var myObj = {
    prop: 'foo'
};

createStub(myObj, 'prop').get(function getterFn() {
    return 'bar';
});

myObj.example; // 'bar'

stub.set(setterFn)

Defines a new setter for this stub.

var myObj = {
    example: 'oldValue',
    prop: 'foo'
};

createStub(myObj, 'prop').set(function setterFn(val) {
    myObj.example = val;
});

myObj.prop = 'baz';

myObj.example; // 'baz'

However, by looking at the spy API we've got no alternatives for this.

I'd be more than happy to implement this, however, we've gotta define what's the API going to be like. I'll open an issue for this.

EDIT: Issue #1392 is related to this.*

@muuki88
Copy link

muuki88 commented May 4, 2017

Thanks a lot @lucasfcosta . This works for me. The confusing was due to the missing typescript definitions for the get and set methods on the stub. I tried the callsFake, which didn't worked as expected.

Should I open an issue for adding the type signatures to the SinonStub interface?

}
},

"it is possible to spy setters": function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation is off here. This should have the same indentation as the test above it and the preceding closing brace should be moved below this test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants