-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
sometimes method fails to be mocked #634
Comments
Thanks for reporting this - it does seem very odd. I suspect the problem is related to I haven't had a lot of time, but I have confirmed that whether or not the top-level Out of interest, in your real scenario can you stub the method that returns the |
@tomstuart has pointed out to me that methods defined on the top-level def foobar = 42
Object.private_instance_methods.grep(/foo/) # => [:foobar]
Object.new.send(:foobar) # => 42 This is almost certainly the root cause of the problem, but I'll need to spend some more time digging into it... |
wrt original use case, it was easy enough to just change the config instead of stubbing. Stubbing it is a little bit more lazy so probably that's why original author of the test used this approach. So no issue with that. So you are right, I noticed the # frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "minitest"
gem "mocha", "2.1.0"
end
require "minitest/autorun"
require "mocha/minitest"
def timeout
end
class UsingMethodMissing
def method_missing(name, *args)
name == :timeout ? name : super
end
end
class BugTest < Minitest::Test
def test_stuff
config = UsingMethodMissing.new
assert_equal :timeout, config.timeout
config.stubs(:timeout)
assert_nil config.timeout
end
end |
Probably you already know that, but mocha 1.x series didn't exhibit this problem. I've hit it on upgrade. |
That's useful to know - thanks. |
I've got a bit further with this... Firstly, it's important to realise that before Mocha gets involved, the It seems as if Mocha is successfully stubbing the method, but because it sees the private method on The bug in Mocha is that it should be calling |
I've created a less pathalogical failing acceptance test to demonstrate the problem in https://github.com/freerange/mocha/compare/fix-stubbing-method-implemented-by-method-missing. It's worth noting that a private method in a superclass is enough to cause the problem - no need for the method on the top-level |
I wonder what the right resolution would be. Current behavior seems that private methods are stubbed as private and public ones are stubbed as public. But in this case mocha doesn't seem to have a good way to know what the intention is. In my opinion it makes sense in such situation (private method available and at the same time a custom |
There is a really weird issue trying to stub a particular method. It can be reproduced by running
ruby test.rb
And
test.rb
is:I would expect this test to pass. But in fact it fails with
Expected 4 to be nil.
The text was updated successfully, but these errors were encountered: