-
-
Notifications
You must be signed in to change notification settings - Fork 158
-
-
Notifications
You must be signed in to change notification settings - Fork 158
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 method leaves behind a copy in the singleton class #20
Comments
I think there may well be some short-comings in what Mocha is doing, but in trying to reproduce the error, I noticed that by creating the instance of
Hopefully this gives you a workaround for the moment. Let me know whether this helps and in the meantime I'll try and do some more investigation. Thanks, James. |
Thanks for the quick response. In the simplified case given here, it is indeed easy to move the global state into the tests. Unfortunately, the object in question in our code isn't created in the test setup, and is also expensive to re-create after every test. In any case, we've worked around the problem by removing the only other place in our code where we create an instance of the class, eliminating the need for an any_instance stub at all. |
Hi Nick. I've done a little bit more looking into this, although not as much as I would like. I'm not sure that your explanation for this issue is correct :-
I think that StubInstanceMethodTest and StubAnyInstanceTest both already check that this is not the case. If you can see something I'm missing, please write a similar test that demonstrates a method being left behind. Following on from your first statement, you say :-
It is certainly true that if you stub the same method via an instance and via any_instance, then the instance stub will take precedence. But I think this makes sense from a Ruby point of view. I have added a Lighthouse ticket to improve the documentation for Mocha::ClassMethods.any_instance. Lastly, you say :-
The behaviour of this line depends on the value of Having said all the above, there does still seem to be something amiss. I'm just not sure that your explanation is correct. I'll try and find some time to do some more digging. Thanks again for reporting the issue. Regards, James. |
I have a test at the head of my fork (nicklewis/mocha@06889b5) that demonstrates this. I see there is a test in
I agree that this behavior is correct if the instance itself and any_instance are both stubbed at the same time. In this case, however, the instance itself has been unstubbed, and the any_instance stub is still not seen.
In the case where https://gist.github.com/804871 Nick |
Hi Nick. That looks really useful. I have a vague recollection that at one point InstanceMethod had its own implementation of restore_original_method. It might have been the victim of some over-zealous refactoring. I'll try and find some time to look at this over the next few days. Cheers, James. |
…ce method, there's no need to move it out the way and then back again. Note that this fixes Github issue #20, because the original method is left unchanged. However, there are other possible issues that this brings to light e.g. what happens if you stub a method that is already defined as a singleton method, but that's for a future commit.
Hi Nick, As you can see on the Github page for this issue, I've attempted a fix in f1d182e. It's a fairly naive and simplistic fix and has highlighted other problems in my mind, but it would be great if you can confirm that it fixes the problem you were having. I'd rather not do a release until I've done some more work, so let me know if you need help trying out the "edge" version of Mocha. Please close the issue if you think it's fixed. Thanks for your patience. Cheers, James. |
Hi Nick, Given that I fixed the failing test (although it's now been subsumed by different tests), I'm going to assume the original problem is fixed and close this issue. Please re-open it if I'm wrong. Thanks again for reporting the problem. Cheers, James. |
Mocha is leaving behind a copy of stubbed methods in the singleton class of the object on which the method is stubbed. So a later stub to any_instance of the class isn't seen by the original object. The following rspec test case illustrates the problem:
A work-around is to add another "test" after the one that adds the stub, which will remove the method from the singleton class.
The cause appears to be at lib/mocha/class_method.rb:54, which restores the original method into the singleton class, regardless of whether that was where it came from in the first place. I'll work on a patch if I get the time.
Incidentally, the same line of code seems to be the perpetrator of #3.
Thanks.
The text was updated successfully, but these errors were encountered: