Skip to content

Instantly share code, notes, and snippets.

@pcreux
Forked from txus/delegate_matcher.rb
Created December 1, 2011 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pcreux/1416136 to your computer and use it in GitHub Desktop.
Save pcreux/1416136 to your computer and use it in GitHub Desktop.
RSpec matcher for delegations
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment