Class Spec::Mocks::Proxy

  1. lib/spec/mocks/proxy.rb
Parent: Object

Constants

DEFAULT_OPTIONS = { :null_object => false, }

Public class methods

allow_message_expectations_on_nil ()
[show 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={})
[show 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
        @expectation_ordering = OrderGroup.new @error_generator
        @expectations = []
        @messages_received = []
        @stubs = []
        @proxied_methods = []
        @options = options ? DEFAULT_OPTIONS.dup.merge(options) : DEFAULT_OPTIONS
      end

Public instance methods

add_message_expectation (expected_from, sym, opts={}, &block)
[show source]
# File lib/spec/mocks/proxy.rb, line 39
      def add_message_expectation(expected_from, sym, opts={}, &block)        
        __add sym
        warn_if_nil_class sym
        if existing_stub = @stubs.detect {|s| s.sym == sym }
          expectation = existing_stub.build_child(expected_from, block_given?? block : nil, 1, opts)
        else
          expectation = MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts)
        end
        @expectations << expectation
        @expectations.last
      end
add_negative_message_expectation (expected_from, sym, &block)
[show source]
# File lib/spec/mocks/proxy.rb, line 51
      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={})
[show source]
# File lib/spec/mocks/proxy.rb, line 58
      def add_stub(expected_from, sym, opts={})
        __add sym
        @stubs.unshift MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, nil, :any, opts)
        @stubs.first
      end
as_null_object ()
[show source]
# File lib/spec/mocks/proxy.rb, line 34
      def as_null_object
        @options[:null_object] = true
        @target
      end
has_negative_expectation? (sym)
[show source]
# File lib/spec/mocks/proxy.rb, line 82
      def has_negative_expectation?(sym)
        @expectations.detect {|expectation| expectation.negative_expectation_for?(sym)}
      end
message_received (sym, *args, &block)
[show source]
# File lib/spec/mocks/proxy.rb, line 90
      def message_received(sym, *args, &block)
        expectation = find_matching_expectation(sym, *args)
        stub = find_matching_method_stub(sym, *args)

        if (stub && expectation && expectation.called_max_times?) || (stub && !expectation)
          if expectation = find_almost_matching_expectation(sym, *args)
            expectation.advise(args, block) unless expectation.expected_messages_received?
          end
          stub.invoke(args, block)
        elsif expectation
          expectation.invoke(args, block)
        elsif expectation = find_almost_matching_expectation(sym, *args)
          expectation.advise(args, block) if null_object? unless expectation.expected_messages_received?
          raise_unexpected_message_args_error(expectation, *args) unless (has_negative_expectation?(sym) or null_object?)
        else
          @target.__send__ :method_missing, sym, *args, &block
        end
      end
null_object? ()
[show source]
# File lib/spec/mocks/proxy.rb, line 30
      def null_object?
        @options[:null_object]
      end
raise_unexpected_message_args_error (expectation, *args)
[show source]
# File lib/spec/mocks/proxy.rb, line 109
      def raise_unexpected_message_args_error(expectation, *args)
        @error_generator.raise_unexpected_message_args_error expectation, *args
      end
raise_unexpected_message_error (sym, *args)
[show source]
# File lib/spec/mocks/proxy.rb, line 113
      def raise_unexpected_message_error(sym, *args)
        @error_generator.raise_unexpected_message_error sym, *args
      end
received_message? (sym, *args, &block)
[show source]
# File lib/spec/mocks/proxy.rb, line 78
      def received_message?(sym, *args, &block)
        @messages_received.any? {|array| array == [sym, args, block]}
      end
record_message_received (sym, args, block)
[show source]
# File lib/spec/mocks/proxy.rb, line 86
      def record_message_received(sym, args, block)
        @messages_received << [sym, args, block]
      end
reset ()
[show source]
# File lib/spec/mocks/proxy.rb, line 70
      def reset
        clear_expectations
        clear_stubs
        reset_proxied_methods
        clear_proxied_methods
        reset_nil_expectations_warning
      end