Skip to content

Instantly share code, notes, and snippets.

@peteryates
Forked from pcreux/delegate_matcher.rb
Last active August 29, 2015 13:57
Show Gist options
  • Save peteryates/9419643 to your computer and use it in GitHub Desktop.
Save peteryates/9419643 to your computer and use it in GitHub Desktop.
Updated to new RSpec syntax
# 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