Sha256: 78417eab4dedb125fb4071b2fb53a6e2f96d2547b6871a75c19eeb5757525bb9

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# RSpec matcher to spec delegations.
#
# Usage:
#
#     describe Post do
#       it { should delegate(:name).to(:author).with_prefix } # post.author_name
#       it { should delegate(:month).to(:created_at) }
#       it { should delegate(:year).to(:created_at) }
#     end

RSpec::Matchers.define :delegate do |target_method|
  match do |delegator|
    source_method = @prefix ? :"#{@to}_#{target_method}" : target_method
    @delegator = delegator
    begin
      @delegator.send(@to)
    rescue NoMethodError
      raise "#{@delegator} does not respond to #{@to}!"
    end
    allow(@delegator).to receive(@to).and_return(double 'receiver', target_method => :called)
    args = (target_method =~ /[^\]]=$/) ? [:called] : []
    @delegator.send(source_method, *args) == :called
  end

  description do
    "delegate :#{target_method} to its #{@to}#{@prefix ? ' with prefix' : ''}"
  end

  failure_message_for_should do |text|
    "expected #{@delegator} to delegate :#{target_method} to its #{@to}#{@prefix ? ' with prefix' : ''}"
  end

  failure_message_for_should_not do |text|
    "expected #{@delegator} not to delegate :#{target_method} to its #{@to}#{@prefix ? ' with prefix' : ''}"
  end

  chain(:to) { |receiver| @to = receiver }
  chain(:with_prefix) { @prefix = true }

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
memory_model-1.0.0 spec/support/delegate_matcher.rb
memory_model-0.1.0 spec/support/delegate_matcher.rb