Skip to content

Instantly share code, notes, and snippets.

@maxjustus
Created January 30, 2012 23:47
Show Gist options
  • Save maxjustus/1707590 to your computer and use it in GitHub Desktop.
Save maxjustus/1707590 to your computer and use it in GitHub Desktop.
handy rspec it_delegates matcher
module RSpec::Example::DelegatesMatcher
def it_delegates(*args)
options = args.pop
if options.is_a?(Hash)
options.symbolize_keys!
delegated_instance = options.fetch(:to) { raise 'You must provide a :to option to it_delegates' }
else
raise 'You must provide an options hash to it_delegates'
end
args.each do |method|
describe method do
it "delegates #{method} to #{delegated_instance}" do
instance = stub(delegated_instance)
subject.stub(delegated_instance) { instance }
args = stub('Args')
result = stub('Result')
instance.should_receive(method).with(args) { result }
eval("subject.#{method}(args)").should == result
end
end
end
end
end
class Some
delegate :thing, :to => :otherthing
def otherthing
Class.new do
def thing(man)
man
end
end
end
end
describe Some do
it_delegates(:thing, :to => :otherthing)
end
RSpec.configure do |config|
config.extend(RSpec::Example::DelegatesMatcher)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment