class RSpec::Mocks::Matchers::Receive

Public Class Methods

new(message, block) click to toggle source
# File lib/rspec/mocks/matchers/receive.rb, line 5
def initialize(message, block)
  @message                 = message
  @block                   = block
  @recorded_customizations = []

  # MRI, JRuby and RBX report the caller inconsistently; MRI
  # reports an extra "in `new'" line in the backtrace that the
  # others do not include. The safest way to find the right
  # line is to search for the first line BEFORE rspec/mocks/syntax.rb.
  @backtrace_line          = caller.find do |line|
    !line.split(':').first.end_with?('rspec/mocks/syntax.rb')
  end
end

Public Instance Methods

does_not_match?(subject, &block)
matches?(subject, &block)
Alias for: setup_expectation
setup_allowance(subject, &block) click to toggle source
# File lib/rspec/mocks/matchers/receive.rb, line 33
def setup_allowance(subject, &block)
  setup_mock_proxy_method_substitute(subject, :add_stub, block)
end
setup_any_instance_allowance(subject, &block) click to toggle source
# File lib/rspec/mocks/matchers/receive.rb, line 45
def setup_any_instance_allowance(subject, &block)
  setup_any_instance_method_substitute(subject, :stub, block)
end
setup_any_instance_expectation(subject, &block) click to toggle source
# File lib/rspec/mocks/matchers/receive.rb, line 37
def setup_any_instance_expectation(subject, &block)
  setup_any_instance_method_substitute(subject, :should_receive, block)
end
setup_any_instance_negative_expectation(subject, &block) click to toggle source
# File lib/rspec/mocks/matchers/receive.rb, line 41
def setup_any_instance_negative_expectation(subject, &block)
  setup_any_instance_method_substitute(subject, :should_not_receive, block)
end
setup_expectation(subject, &block) click to toggle source
# File lib/rspec/mocks/matchers/receive.rb, line 19
def setup_expectation(subject, &block)
  setup_mock_proxy_method_substitute(subject, :add_message_expectation, block)
end
Also aliased as: matches?
setup_negative_expectation(subject, &block) click to toggle source
# File lib/rspec/mocks/matchers/receive.rb, line 24
def setup_negative_expectation(subject, &block)
  # ensure `never` goes first for cases like `never.and_return(5)`,
  # where `and_return` is meant to raise an error
  @recorded_customizations.unshift Customization.new(:never, [], nil)

  setup_expectation(subject, &block)
end
Also aliased as: does_not_match?

Private Instance Methods

setup_any_instance_method_substitute(subject, method, block) click to toggle source
# File lib/rspec/mocks/matchers/receive.rb, line 67
def setup_any_instance_method_substitute(subject, method, block)
  any_instance_recorder = ::RSpec::Mocks.any_instance_recorder_for(subject)
  setup_method_substitute(any_instance_recorder, method, block)
end
setup_method_substitute(host, method, block, *args) click to toggle source
# File lib/rspec/mocks/matchers/receive.rb, line 72
def setup_method_substitute(host, method, block, *args)
  args << @message.to_sym
  expectation = host.__send__(method, *args, &(@block || block))

  @recorded_customizations.each do |customization|
    customization.playback_onto(expectation)
  end
  expectation
end
setup_mock_proxy_method_substitute(subject, method, block) click to toggle source
# File lib/rspec/mocks/matchers/receive.rb, line 62
def setup_mock_proxy_method_substitute(subject, method, block)
  proxy = ::RSpec::Mocks.proxy_for(subject)
  setup_method_substitute(proxy, method, block, @backtrace_line)
end