Parent

Class/Module Index [+]

Quicksearch

Shoulda::Matchers::Independent::DelegateMatcher

@private

Attributes

delegated_arguments[R]
delegating_method[R]
method[R]
method_on_target[R]
subject[R]
subject_double_collection[R]
target_double[R]
target_method[R]

Public Class Methods

new(delegating_method) click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 116
def initialize(delegating_method)
  @delegating_method = delegating_method
  @method_on_target = @delegating_method
  @target_double = Doublespeak::ObjectDouble.new

  @delegated_arguments = []
  @target_method = nil
  @subject = nil
  @subject_double_collection = nil
end

Public Instance Methods

as(method_on_target) click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 148
def as(method_on_target)
  @method_on_target = method_on_target
  self
end
description() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 137
def description
  add_clarifications_to(
    "delegate method ##{delegating_method} to :#{target_method}"
  )
end
failure_message() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 158
def failure_message
  base = "Expected #{formatted_delegating_method_name} to delegate to #{formatted_target_method_name}"
  add_clarifications_to(base)
  base << "\nCalls on #{formatted_target_method_name}:"
  base << formatted_calls_on_target
  base.strip
end
Also aliased as: failure_message_for_should
failure_message_for_should() click to toggle source
Alias for: failure_message
failure_message_for_should_not() click to toggle source
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 167
def failure_message_when_negated
  base = "Expected #{formatted_delegating_method_name} not to delegate to #{formatted_target_method_name}"
  add_clarifications_to(base)
  base << ', but it did'
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 127
def matches?(subject)
  @subject = subject

  ensure_target_method_is_present!

  subject_has_delegating_method? &&
    subject_has_target_method? &&
    subject_delegates_to_target_correctly?
end
to(target_method) click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 143
def to(target_method)
  @target_method = target_method
  self
end
with_arguments(*arguments) click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 153
def with_arguments(*arguments)
  @delegated_arguments = arguments
  self
end

Protected Instance Methods

add_clarifications_to(message) click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 186
def add_clarifications_to(message)
  if delegated_arguments.any?
    message << " with arguments: #{delegated_arguments.inspect}"
  end

  if method_on_target != delegating_method
    message << " as ##{method_on_target}"
  end

  message
end
calls_on_target() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 265
def calls_on_target
  target_double.calls
end
calls_to_method_on_target() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 261
def calls_to_method_on_target
  target_double.calls_to(method_on_target)
end
ensure_target_method_is_present!() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 232
def ensure_target_method_is_present!
  if target_method.blank?
    raise TargetNotDefinedError
  end
end
formatted_calls_on_target() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 269
def formatted_calls_on_target
  string = ""

  if calls_on_target.any?
    string << "\n"
    calls_on_target.each_with_index do |call, i|
      name = call.method_name
      args = call.args.map { |arg| arg.inspect }.join(', ')
      string << "#{i+1}) #{name}(#{args})\n"
    end
  else
    string << " (none)"
  end

  string
end
formatted_delegating_method_name() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 198
def formatted_delegating_method_name
  formatted_method_name_for(delegating_method)
end
formatted_method_name_for(method_name) click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 206
def formatted_method_name_for(method_name)
  if subject.is_a?(Class)
    subject.name + '.' + method_name.to_s
  else
    subject.class.name + '#' + method_name.to_s
  end
end
formatted_target_method_name() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 202
def formatted_target_method_name
  formatted_method_name_for(target_method)
end
register_subject_double_collection() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 252
def register_subject_double_collection
  double_collection =
    Doublespeak.double_collection_for(subject.singleton_class)
  double_collection.register_stub(target_method).
    to_return(target_double)

  @subject_double_collection = double_collection
end
subject_delegates_to_target_correctly?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 238
def subject_delegates_to_target_correctly?
  register_subject_double_collection

  Doublespeak.with_doubles_activated do
    subject.public_send(delegating_method, *delegated_arguments)
  end

  if delegated_arguments.any?
    target_received_method_with_delegated_arguments?
  else
    target_received_method?
  end
end
subject_has_delegating_method?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 224
def subject_has_delegating_method?
  subject.respond_to?(delegating_method)
end
subject_has_target_method?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 228
def subject_has_target_method?
  subject.respond_to?(target_method)
end
target_received_method?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 214
def target_received_method?
  calls_to_method_on_target.any?
end
target_received_method_with_delegated_arguments?() click to toggle source
# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 218
def target_received_method_with_delegated_arguments?
  calls_to_method_on_target.any? do |call|
    call.args == delegated_arguments
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.