rspec-mocks icon indicating copy to clipboard operation
rspec-mocks copied to clipboard

Using mocks on methods with refinements fails

Open dreyks opened this issue 8 years ago • 3 comments

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 avatar Jun 22 '16 22:06 dreyks