Created
January 30, 2012 23:47
-
-
Save maxjustus/1707590 to your computer and use it in GitHub Desktop.
handy rspec it_delegates matcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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