bogus icon indicating copy to clipboard operation
bogus copied to clipboard

Duck types are sensitive to argument names

Open psyho opened this issue 8 years ago • 3 comments

For example:

class Foo
  def call(env)
  end
end

class Bar
  def call(_)
  end
end

fake(:xxx) { [Foo, Bar]}.respond_to?(:call) == false

psyho avatar Sep 01 '15 12:09 psyho

Is there any workaround for this issue? I have a bunch of classes and some define foo through delegate and other classes define foo through attr_reader – Bogus seems to have a problem with this as well.

Edit: For now I can "manually" define the delegated methods, though I'm more interested in a Bogus-specific workaround for this problem.

ravicious avatar Nov 14 '16 14:11 ravicious

There is no workaround for this, although the fix would be fairly simple - you can just strip argument names from the parameters list when comparing here: https://github.com/psyho/bogus/blob/master/lib/bogus/fakes/makes_ducks.rb#L33

However, this might not work in your particular case. If I remember correctly, delegate defines a method that accepts an arbitrary number of arguments, so the method signature is actually different.

psyho avatar Nov 14 '16 14:11 psyho

However, this might not work in your particular case. If I remember correctly, delegate defines a method that accepts an arbitrary number of arguments, so the method signature is actually different.

You're right, I didn't know about it, thanks! I think it's time to rethink the usage of the RuboCop Rails/Delegate cop in our project.

ravicious avatar Nov 14 '16 15:11 ravicious