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
Define respond_to_missing? on Mocha::Mock #323
Conversation
@tjvc Thanks for digging into this a bit more. There are already places in the code which do something different for older Ruby versions. Can I suggest you add |
Fixes freerange#321. As of Ruby 2.4, Forwardable warns when delegating to a private method. Warnings are shown when delegating methods to a Mocha::Mock. To determine whether to show a warning, Forwardable tests the delegated method with the defined? operator. Mocha uses method_missing to respond to mocked methods. defined? uses respond_to_missing? to determine whether or not a missing method is defined. Defining respond_to_missing? on Mocha::Mock, in place of respond_to?, prevents warnings from being shown. To preserve compatibility with Ruby 1.8.7, we continue to conditionally define respond_to?. Prior to Ruby 1.9.3, defined? returns nil for method calls handled via method_missing, so we also only test that mocked methods are defined? in later Ruby versions.
3f61937
to
befb416
Compare
Thanks for the pointer @floehopper. I've updated the code to conditionally define |
@tjvc Thanks for updating the PR. I'll try to take a look as soon as I have time. |
Hi @floehopper, was just wondering if you'd had a chance to take a look at this yet? |
@tjvc Sorry for the delay - we've been very busy with client work. This looks great to me! I'll merge it now. |
I'll try to get a release out in the next few days including this change. |
No worries, @floehopper. Thanks for merging! 👍 |
I've just released v1.4.0 which includes this fix. |
Fixes #321.
As of Ruby 2.4,
Forwardable
warns when delegating to a private method.Warnings are shown when delegating methods to a
Mocha::Mock
.To determine whether to show a warning, Forwardable tests the delegated
method with the
defined?
operator. Mocha usesmethod_missing
to respondto mocked methods.
defined?
usesrespond_to_missing?
to determinewhether or not a missing method is defined. Defining
respond_to_missing?
on
Mocha::Mock
, in place ofrespond_to?
, prevents warnings from beingshown.
To preserve compatibility with Ruby 1.8.7, we continue to conditionally
define
respond_to?
. Prior to Ruby 1.9.3,defined?
returnsnil
for methodcalls handled via
method_missing
, so we also only test that mockedmethods are
defined?
in later Ruby versions.