Parent

Class/Module Index [+]

Quicksearch

Spec::Mocks::Proxy

Constants

DEFAULT_OPTIONS

Public Class Methods

allow_message_expectations_on_nil() click to toggle source
# File lib/spec/mocks/proxy.rb, line 10
def self.allow_message_expectations_on_nil
  @@warn_about_expectations_on_nil = false
  
  # ensure nil.rspec_verify is called even if an expectation is not set in the example
  # otherwise the allowance would effect subsequent examples
  $rspec_mocks.add(nil) unless $rspec_mocks.nil?
end
new(target, name=nil, options={}) click to toggle source
# File lib/spec/mocks/proxy.rb, line 18
def initialize(target, name=nil, options={})
  @target = target
  @name = name
  @error_generator = ErrorGenerator.new target, name, options
  @expectation_ordering = OrderGroup.new @error_generator
  @expectations = []
  @messages_received = []
  @stubs = []
  @proxied_methods = []
  @options = options ? DEFAULT_OPTIONS.dup.merge(options) : DEFAULT_OPTIONS
  @already_proxied_respond_to = false
end

Public Instance Methods

add_message_expectation(expected_from, sym, opts={}, &block) click to toggle source
# File lib/spec/mocks/proxy.rb, line 40
def add_message_expectation(expected_from, sym, opts={}, &block)        
  __add sym
  warn_if_nil_class sym
  @expectations << build_expectation(expected_from, sym, opts, &block)
  @expectations.last
end
add_negative_message_expectation(expected_from, sym, &block) click to toggle source
# File lib/spec/mocks/proxy.rb, line 55
def add_negative_message_expectation(expected_from, sym, &block)
  __add sym
  warn_if_nil_class sym
  @expectations << NegativeMessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil)
  @expectations.last
end
add_stub(expected_from, sym, opts={}, &implementation) click to toggle source
# File lib/spec/mocks/proxy.rb, line 62
def add_stub(expected_from, sym, opts={}, &implementation)
  __add sym
  @stubs.unshift MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, nil, :any, opts, &implementation)
  @stubs.first
end
as_null_object() click to toggle source
# File lib/spec/mocks/proxy.rb, line 35
def as_null_object
  @options[:null_object] = true
  @target
end
build_expectation(expected_from, sym, opts, &block) click to toggle source
# File lib/spec/mocks/proxy.rb, line 47
def build_expectation(expected_from, sym, opts, &block)
  if stub = find_matching_method_stub(sym) 
    stub.build_child(expected_from, block_given?? block : nil, 1, opts)
  else
    MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts)
  end
end
find_matching_method_stub(sym, *args) click to toggle source
# File lib/spec/mocks/proxy.rb, line 149
def find_matching_method_stub(sym, *args)
  @stubs.find {|stub| stub.matches(sym, args)}
end
has_negative_expectation?(sym) click to toggle source
# File lib/spec/mocks/proxy.rb, line 96
def has_negative_expectation?(sym)
  @expectations.any? {|expectation| expectation.negative_expectation_for?(sym)}
end
invoke_expectation(expectation, *args, &block) click to toggle source
# File lib/spec/mocks/proxy.rb, line 126
def invoke_expectation(expectation, *args, &block)
  expectation.invoke(*args, &block)
end
message_received(sym, *args, &block) click to toggle source
# File lib/spec/mocks/proxy.rb, line 104
def message_received(sym, *args, &block)
  expectation = find_matching_expectation(sym, *args)
  stub = find_matching_method_stub(sym, *args)

  if ok_to_invoke_stub?(stub, expectation)
    record_stub(stub, sym, args, &block)
  elsif expectation
    invoke_expectation(expectation, *args, &block)
  elsif expectation = find_almost_matching_expectation(sym, *args)
    record_almost_matching_expectation(expectation, sym, *args, &block)
  else
    @target.__send__ :method_missing, sym, *args, &block
  end
end
null_object?() click to toggle source
# File lib/spec/mocks/proxy.rb, line 31
def null_object?
  @options[:null_object]
end
ok_to_invoke_stub?(stub, expectation) click to toggle source
# File lib/spec/mocks/proxy.rb, line 137
def ok_to_invoke_stub?(stub, expectation)
  stub && (!expectation || expectation.called_max_times?)
end
raise_unexpected_message_args_error(expectation, *args) click to toggle source
# File lib/spec/mocks/proxy.rb, line 141
def raise_unexpected_message_args_error(expectation, *args)
  @error_generator.raise_unexpected_message_args_error expectation, *args
end
raise_unexpected_message_error(sym, *args) click to toggle source
# File lib/spec/mocks/proxy.rb, line 145
def raise_unexpected_message_error(sym, *args)
  @error_generator.raise_unexpected_message_error sym, *args
end
received_message?(sym, *args, &block) click to toggle source
# File lib/spec/mocks/proxy.rb, line 92
def received_message?(sym, *args, &block)
  @messages_received.any? {|array| array == [sym, args, block]}
end
record_almost_matching_expectation(expectation, sym, *args, &block) click to toggle source
# File lib/spec/mocks/proxy.rb, line 130
def record_almost_matching_expectation(expectation, sym, *args, &block)
  expectation.advise(args, block)
  unless (null_object? or has_negative_expectation?(sym))
    raise_unexpected_message_args_error(expectation, *args)
  end
end
record_message_received(sym, args, block) click to toggle source
# File lib/spec/mocks/proxy.rb, line 100
def record_message_received(sym, args, block)
  @messages_received << [sym, args, block]
end
record_stub(stub, sym, args, &block) click to toggle source
# File lib/spec/mocks/proxy.rb, line 119
def record_stub(stub, sym, args, &block)
  almost_matching_expectation(sym, *args) do |e|
    e.advise(args, block)
  end
  stub.invoke(*args, &block)
end
remove_stub(message) click to toggle source
# File lib/spec/mocks/proxy.rb, line 68
def remove_stub(message)
  message = message.to_sym
  if stub_to_remove = @stubs.detect { |s| s.matches_name?(message) }
    reset_proxied_method(message)
    @stubs.delete(stub_to_remove)
  else
    raise MockExpectationError, "The method `#{message}` was not stubbed or was already unstubbed"
  end
end
reset() click to toggle source
# File lib/spec/mocks/proxy.rb, line 84
def reset
  clear_expectations
  clear_stubs
  reset_proxied_methods
  clear_proxied_methods
  reset_nil_expectations_warning
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.