moonmaster9000 / quacky

make your doubles quack like a real duck.
MIT License
5 stars 2 forks source link

Can't specify the arguments to initialize #15

Open zephyr-dev opened 11 years ago

zephyr-dev commented 11 years ago

It'd be nice if you could specify the arity of initializing a duck type.

I tried just defining an initialize method in the interface module:

module Quacky::Interfaces
  module ActivityFeedItem
    def initialize(item, view_context); end
  end
end

but now I get

    Failure/Error: it { should quack_like Quacky::Interfaces::ActivityFeedItem }
     ArgumentError:
       wrong number of arguments (0 for 2)
     # ./spec/support/interfaces/activity_feed_item.rb:3:in `initialize'
     # /Users/pivotal/.rvm/gems/ruby-1.9.3-p327/gems/quacky-0.2.7/lib/quacky/duck_type_verifier.rb:41:in `new'
     # /Users/pivotal/.rvm/gems/ruby-1.9.3-p327/gems/quacky-0.2.7/lib/quacky/duck_type_verifier.rb:41:in `duck_type_object'
     # /Users/pivotal/.rvm/gems/ruby-1.9.3-p327/gems/quacky-0.2.7/lib/quacky/duck_type_verifier.rb:32:in `duck_type_methods'
     # /Users/pivotal/.rvm/gems/ruby-1.9.3-p327/gems/quacky-0.2.7/lib/quacky/duck_type_verifier.rb:10:in `verify!'
     # /Users/pivotal/.rvm/gems/ruby-1.9.3-p327/gems/quacky-0.2.7/lib/quacky/rspec_setup.rb:14:in `block (3 levels) in <top (required)>'
     # /Users/pivotal/.rvm/gems/ruby-1.9.3-p327/gems/rspec-expectations-2.12.1/lib/rspec/matchers/extensions/instance_eval_with_args.rb:11:in `instance_exec'
benmoss commented 11 years ago

Turns out to be slightly difficult because initialize is considered private. Not sure what the best solution would be, maybe special-casing initialize? It is the one private method that is still pretty much guaranteed to be part of the public contract, in the sense that its arity matters.

squeedee commented 11 years ago

It would have to be a special case where you use instance_method instead of public_method.

Only problem I see, anyone using quacky who hasn't worried about initialize, but the initialize of the test class has arity > 0, would suddenly get an arity error.

You can't tell if the Interface module specified the initialize or not - if its not defined, it's there with an arity of 0.