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
Warning when delegating to mock in Ruby 2.4 #321
Comments
Thanks for reporting this with such a clear example. As far as I can see this is a problem with Ruby and not Mocha, because I see the same behaviour when I replace the mock object with a simple Ruby class using
class Baz
def method_missing(symbol, *args)
if symbol == :bar
123
else
super
end
end
def respond_to_missing?(symbol, include_private = false)
(symbol == :bar) || super
end
end
require "minitest/autorun"
require_relative "foo"
require_relative "baz"
class FooTest < Minitest::Test
def test_delegates_bar_to_baz
baz = Baz.new
foo = Foo.new(baz)
foo.bar
end
end
Does that make sense? I think the next step would be to report the issue to Ruby - are you happy to do that or would you like me to do it? |
FWIW I think the problem lies on this line of |
Just to clarify, I would consider
|
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, which uses method_missing, a private method, to respond to calls to mocked methods. Defining respond_to_missing? on the receiver, in place of respond_to?, prevents warnings from being shown.
Thanks for getting back to me, @floehopper. I did a bit more digging: it looks like an issue was already reported for Ruby, with the suggested fix being to implement I opened a pull request with the change (#323), but I can see that this has broken compatibility with Ruby 1.8.7, where |
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.
Ruby 2.4 warns when delegating to a private method with
Forwardable
: https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/56210.When delegating to a Mocha mock object with
Forwardable
, a "forwarding to private method" warning is shown.For example, given:
The test will output:
Ideally these warnings would not be shown, because in most cases the expectation is set on a public method.
The text was updated successfully, but these errors were encountered: