# 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
# 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
# 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
# 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
# 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
# File lib/spec/mocks/proxy.rb, line 35 def as_null_object @options[:null_object] = true @target end
# 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
# File lib/spec/mocks/proxy.rb, line 149 def find_matching_method_stub(sym, *args) @stubs.find {|stub| stub.matches(sym, args)} end
# File lib/spec/mocks/proxy.rb, line 96 def has_negative_expectation?(sym) @expectations.any? {|expectation| expectation.negative_expectation_for?(sym)} end
# File lib/spec/mocks/proxy.rb, line 126 def invoke_expectation(expectation, *args, &block) expectation.invoke(*args, &block) end
# 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
# File lib/spec/mocks/proxy.rb, line 31 def null_object? @options[:null_object] end
# File lib/spec/mocks/proxy.rb, line 137 def ok_to_invoke_stub?(stub, expectation) stub && (!expectation || expectation.called_max_times?) end
# 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
# File lib/spec/mocks/proxy.rb, line 145 def raise_unexpected_message_error(sym, *args) @error_generator.raise_unexpected_message_error sym, *args end
# File lib/spec/mocks/proxy.rb, line 92 def received_message?(sym, *args, &block) @messages_received.any? {|array| array == [sym, args, block]} end
# 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
# File lib/spec/mocks/proxy.rb, line 100 def record_message_received(sym, args, block) @messages_received << [sym, args, block] end
# 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
# 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
Generated with the Darkfish Rdoc Generator 2.