Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# RSpec matcher to spec delegations.
# Forked from https://gist.github.com/ssimeonov/5942729 with fixes
# for arity + custom prefix.
#
# Usage:
#
# describe Post do
# it { should delegate(:name).to(:author).with_prefix } # post.author_name
# it { should delegate(:name).to(:author).with_prefix(:any) } # post.any_name
# it { should delegate(:month).to(:created_at) }
# it { should delegate(:year).to(:created_at) }
# it { should delegate(:something).to(:'@instance_var') }
# end
RSpec::Matchers.define :delegate do |method|
match do |delegator|
@method = @prefix ? :"#{@prefix}_#{method}" : method
@delegator = delegator
if @to.to_s[0] == '@'
# Delegation to an instance variable
old_value = @delegator.instance_variable_get(@to)
begin
@delegator.instance_variable_set(@to, receiver_double(method))
@delegator.send(@method) == :called
ensure
@delegator.instance_variable_set(@to, old_value)
end
elsif @delegator.respond_to?(@to, true)
unless [0,-1].include?(@delegator.method(@to).arity)
raise "#{@delegator}'s' #{@to} method does not have zero or -1 arity (it expects parameters)"
end
allow(@delegator).to receive(@to).and_return(receiver_double(method))
@delegator.send(@method) == :called
else
raise "#{@delegator} does not respond to #{@to}"
end
end
description do
"delegate :#{@method} to its #{@to}#{@prefix ? ' with prefix' : ''}"
end
failure_message do |text|
"expected #{@delegator} to delegate :#{@method} to its #{@to}#{@prefix ? ' with prefix' : ''}"
end
failure_message_when_negated do |text|
"expected #{@delegator} not to delegate :#{@method} to its #{@to}#{@prefix ? ' with prefix' : ''}"
end
chain(:to) { |receiver| @to = receiver }
chain(:with_prefix) { |prefix| @prefix = prefix || @to }
def receiver_double(method)
double('receiver').tap do |receiver|
allow(receiver).to receive(method).and_return(:called)
end
end
end
@joeytheman
Copy link
Author

joeytheman commented Jul 2, 2014

Updated for expect syntax

Loading

@barnett
Copy link

barnett commented Jul 2, 2014

I am getting the following error with this:

 Failure/Error: it { should delegate(:name).to(:responder).with_prefix }
     NoMethodError:
       undefined method `failure_message' for #<RSpec::Matchers::DSL::Matcher:0x0000010553e5e8>

Loading

@lfzawacki
Copy link

lfzawacki commented Jul 17, 2014

@blklane this is probably because you're still using rspec 2 and the method is not called failure_message, try the older revisions of the gist https://gist.github.com/joeytheman/0fe021821e4c62f552ce/revisions

Loading

@thaichor
Copy link

thaichor commented Dec 9, 2014

using RSpec 3.1, errors wrong argument 0 for 1, if I don't give any argument to #with_prefix

I fixed this by modify line 53 to this. (Not the best fix)

Ruby
chain(:with_prefix) { |*prefix| @Prefix = prefix.first || @to }

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment