rspec-mocks
rspec-mocks copied to clipboard
Using mocks on methods with refinements fails
In a situation when a method comes from an included module and then this same method is refined, invoking expect().to receive fails as if the method is not present at all.
After spending some time in debugger I realised that the expectance itself passes, but the restore_original_visibility fails, because no method was found to change visibility for
Reproduction self-executing snippet. Note that if I move inner method directly into Parent instead of include Inc the example passes without failures
#!/usr/bin/env ruby
module Inc
def inner
'inc'
end
end
class Parent
include Inc
end
module Ref
refine Parent do
def inner
super + ' refined'
end
end
end
class Child < Parent
using Ref
def outer
inner
end
end
require 'rspec/autorun'
describe Child do
describe '#outer' do
it 'calls inner' do
child = Child.new
expect(child).to receive(:inner)
child.outer
end
end
end
@dreyks your example works for me on RSpec master on both 2.4 and 2.2. However, I will note that refinements do not work with any_instance. As per @JonRowe's PR.
If the repro doesn’t fail anymore, is it time to close this?
hmm... I've just rerun the example on both ruby 2.4.1 and 2.2.6 with rspec@master (3.8.pre) and it fails with
NameError:
undefined method `inner' for class `#<Class:#<Child:0x007ffd1785b670>>'
Closing due to inactivity during the monorepo migration, but if its ongoing issue please reopen there.