-
-
Save peteryates/9419643 to your computer and use it in GitHub Desktop.
Updated to new RSpec syntax
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 { expect(subject).to delegate(:title).to(:name) } # post.title => post.name | |
# it { expect(subject).to delegate(:month).to(:created_at) } # post.month => post.created_at | |
# it { expect(subject).to 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 | |
allow(@delegator).to receive_message_chain(@to) { :return_value } | |
@delegator.send(@method).eql?(:return_value) | |
end | |
description do | |
"delegate :#{@method} to #{pretty_to}" | |
end | |
failure_message do |text| | |
"expected #{@delegator} to delegate :#{@method} to #{pretty_to}" | |
end | |
failure_message_when_negated 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