Skip to content

Only restore an instance method on the owning class

Created by: jrafanie

Fixes #1042 (closed)

class Shape
  def area(value)
    value
  end
end

class Circle < Shape
  def area
    super(2)
  end
end

class SmallCircle < Circle
end

When using: allow_any_instance_of(SmallCircle).to receive(:area)

Previously, we'd copy the area method from the first superclass that implements it. In this case, we'd copy Circle#area into SmallCircle with a backup name.

At the end of the example, we'd remove the stubbing and rename the copied method back, causing SmallCircle to implement area when it never did previously.

Any calls to SmallCircle#area would end up calling the contents of Circle#area and the call to super(2) would be sent to the wrong class: Circle, not Shape. In this case, Circle#area doesn't take any arguments so you get an ArgumentError.

Previously, ruby 2.2 and below allowed this strange behavior to work.

This appears to have been "fixed" in ruby in this commit: https://github.com/ruby/ruby/commit/c8854d2ca4be9ee6946e6d17b0e17d9ef130ee81

Merge request reports