Sha256: 9edf0375d629c04bbf4db2b07114b64da93823d510e83a11ead3e6019f363713

Contents?: true

Size: 1.15 KB

Versions: 22

Compression:

Stored size: 1.15 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 |method|
  match do |delegator|
    @method = @prefix ? :"#{@to}_#{method}" : method
    @delegator = delegator
    begin
      @delegator.send(@to)
    rescue NoMethodError
      raise "#{@delegator} does not respond to #{@to}!"
    end
    @delegator.stub(@to => double('receiver'))
    @delegator.send(@to).stub(method => :called)
    @delegator.send(@method) == :called
  end

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

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

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

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

end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
appsignal-0.6.7 spec/support/delegate_matcher.rb
appsignal-0.6.6 spec/support/delegate_matcher.rb
appsignal-0.6.5 spec/support/delegate_matcher.rb
appsignal-0.6.4 spec/support/delegate_matcher.rb
appsignal-0.6.3 spec/support/delegate_matcher.rb
appsignal-0.6.3.beta.3 spec/support/delegate_matcher.rb
appsignal-0.6.3.beta.2 spec/support/delegate_matcher.rb
appsignal-0.6.3.beta.1 spec/support/delegate_matcher.rb
appsignal-0.6.2 spec/support/delegate_matcher.rb
appsignal-0.6.1 spec/support/delegate_matcher.rb
appsignal-0.6.0.beta.2 spec/support/delegate_matcher.rb
appsignal-0.6.0.beta.1 spec/support/delegate_matcher.rb
appsignal-0.5.5 spec/support/delegate_matcher.rb
appsignal-0.5.3 spec/support/delegate_matcher.rb
appsignal-0.5.1 spec/support/delegate_matcher.rb
appsignal-0.5.0 spec/support/delegate_matcher.rb
appsignal-0.4.7 spec/support/delegate_matcher.rb
appsignal-0.4.6 spec/support/delegate_matcher.rb
appsignal-0.4.5 spec/support/delegate_matcher.rb
appsignal-0.4.4 spec/support/delegate_matcher.rb