Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

45 lines (39 sloc) 1.1 kB
RSpec::Matchers.define :delegate do |delegated_method|
chain :to do |target_method|
@target_method = target_method
end
chain :as do |method_on_target|
@method_on_target = method_on_target
end
chain :with_arguments do |args|
@args = args
end
match do |instance|
extend Mocha::API
@instance = instance
@args ||= []
return_value = 'stubbed return value'
method_on_target = @method_on_target || delegated_method
stubbed_target = stub('stubbed_target', method_on_target => return_value)
@instance.stubs(@target_method => stubbed_target)
begin
@instance.send(delegated_method, *@args) == return_value
rescue NoMethodError
false
end
end
failure_message do
if Class === @instance
message = "expected #{@instance.name} "
prefix = '.'
else
message = "expected #{@instance.class.name} "
prefix = '#'
end
message << "to delegate #{prefix}#{delegated_method} to #{prefix}#{@target_method}"
if @method_on_target
message << ".#{@method_on_target}"
end
message
end
end
Jump to Line
Something went wrong with that request. Please try again.