-
-
Save pcreux/1416136 to your computer and use it in GitHub Desktop.
RSpec matcher for delegations
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 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