# RSpec matcher to spec delegations. # # Usage: # # describe Post do # it { should delegate(:title).to(:name) } # post.title => post.name # it { should delegate(:month).to(:created_at) } # post.month => post.created_at # it { should delegate(:author_name).to(:author, :name) } # post.author_name => post.author.name # end RSpec::Matchers.define :delegate do |method| match do |delegator| @method = method @delegator = delegator @delegator.stub_chain(@to) { :return_value } @delegator.send(@method) == :return_value end description do "delegate :#{@method} to #{pretty_to}" end failure_message_for_should do |text| "expected #{@delegator} to delegate :#{@method} to #{pretty_to}" end failure_message_for_should_not do |text| "expected #{@delegator} not to delegate :#{@method} to #{pretty_to}" end def pretty_to @to.join('.') end chain(:to) { |*to| @to = to } end