Class: RSpec::Mocks::Proxy

Inherits:
Object show all
Defined in:
lib/rspec/mocks/proxy.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, name = nil, options = {}) ⇒ Proxy

Returns a new instance of Proxy.



26
27
28
29
30
31
32
33
34
35
# File 'lib/rspec/mocks/proxy.rb', line 26

def initialize(object, name=nil, options={})
  @object = object
  @name = name
  @error_generator = ErrorGenerator.new object, name, options
  @expectation_ordering = OrderGroup.new @error_generator
  @messages_received = []
  @options = options
  @already_proxied_respond_to = false
  @null_object = false
end

Class Method Details

.allow_message_expectations_on_nilObject



13
14
15
16
17
18
19
# File 'lib/rspec/mocks/proxy.rb', line 13

def 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::space.add(nil) unless RSpec::Mocks::space.nil?
end

.allow_message_expectations_on_nil?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rspec/mocks/proxy.rb', line 21

def allow_message_expectations_on_nil?
  !warn_about_expectations_on_nil
end

.warn_about_expectations_on_nilObject



5
6
7
# File 'lib/rspec/mocks/proxy.rb', line 5

def warn_about_expectations_on_nil
  defined?(@warn_about_expectations_on_nil) ? @warn_about_expectations_on_nil : true
end

.warn_about_expectations_on_nil=(new_value) ⇒ Object



9
10
11
# File 'lib/rspec/mocks/proxy.rb', line 9

def warn_about_expectations_on_nil=(new_value)
  @warn_about_expectations_on_nil = new_value
end

Instance Method Details

#add_message_expectation(location, method_name, opts = {}, &block) ⇒ Object



56
57
58
# File 'lib/rspec/mocks/proxy.rb', line 56

def add_message_expectation(location, method_name, opts={}, &block)        
  method_double[method_name].add_expectation @error_generator, @expectation_ordering, location, opts, &block
end

#add_negative_message_expectation(location, method_name, &implementation) ⇒ Object



60
61
62
# File 'lib/rspec/mocks/proxy.rb', line 60

def add_negative_message_expectation(location, method_name, &implementation)
  method_double[method_name].add_negative_expectation @error_generator, @expectation_ordering, location, &implementation
end

#add_stub(location, method_name, opts = {}, &implementation) ⇒ Object



64
65
66
# File 'lib/rspec/mocks/proxy.rb', line 64

def add_stub(location, method_name, opts={}, &implementation)
  method_double[method_name].add_stub @error_generator, @expectation_ordering, location, opts, &implementation
end

#already_proxied_respond_toObject

:nodoc:



48
49
50
# File 'lib/rspec/mocks/proxy.rb', line 48

def already_proxied_respond_to # :nodoc:
  @already_proxied_respond_to = true
end

#already_proxied_respond_to?Boolean

:nodoc:

Returns:

  • (Boolean)


52
53
54
# File 'lib/rspec/mocks/proxy.rb', line 52

def already_proxied_respond_to? # :nodoc:
  @already_proxied_respond_to
end

#as_null_objectObject

Tells the object to ignore any messages that aren’t explicitly set as stubs or message expectations.



43
44
45
46
# File 'lib/rspec/mocks/proxy.rb', line 43

def as_null_object
  @null_object = true
  @object
end

#has_negative_expectation?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/rspec/mocks/proxy.rb', line 86

def has_negative_expectation?(method_name)
  method_double[method_name].expectations.detect {|expectation| expectation.negative_expectation_for?(method_name)}
end

#message_received(method_name, *args, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rspec/mocks/proxy.rb', line 94

def message_received(method_name, *args, &block)
  expectation = find_matching_expectation(method_name, *args)
  stub = find_matching_method_stub(method_name, *args)

  if (stub && expectation && expectation.called_max_times?) || (stub && !expectation)
    if expectation = find_almost_matching_expectation(method_name, *args)
      expectation.advise(*args) unless expectation.expected_messages_received?
    end
    stub.invoke(*args, &block)
  elsif expectation
    expectation.invoke(*args, &block)
  elsif expectation = find_almost_matching_expectation(method_name, *args)
    expectation.advise(*args) if null_object? unless expectation.expected_messages_received?
    raise_unexpected_message_args_error(expectation, *args) unless (has_negative_expectation?(method_name) or null_object?)
  elsif stub = find_almost_matching_stub(method_name, *args)
    stub.advise(*args)
    raise_unexpected_message_args_error(stub, *args)
  elsif @object.is_a?(Class)
    @object.superclass.send(method_name, *args, &block)
  else
    @object.__send__(:method_missing, method_name, *args, &block)
  end
end

#null_object?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rspec/mocks/proxy.rb', line 37

def null_object?
  @null_object
end

#raise_unexpected_message_args_error(expectation, *args) ⇒ Object



118
119
120
# File 'lib/rspec/mocks/proxy.rb', line 118

def raise_unexpected_message_args_error(expectation, *args)
  @error_generator.raise_unexpected_message_args_error(expectation, *args)
end

#raise_unexpected_message_error(method_name, *args) ⇒ Object



122
123
124
# File 'lib/rspec/mocks/proxy.rb', line 122

def raise_unexpected_message_error(method_name, *args)
  @error_generator.raise_unexpected_message_error method_name, *args
end

#received_message?(method_name, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/rspec/mocks/proxy.rb', line 82

def received_message?(method_name, *args, &block)
  @messages_received.any? {|array| array == [method_name, args, block]}
end

#record_message_received(method_name, *args, &block) ⇒ Object



90
91
92
# File 'lib/rspec/mocks/proxy.rb', line 90

def record_message_received(method_name, *args, &block)
  @messages_received << [method_name, args, block]
end

#remove_stub(method_name) ⇒ Object



68
69
70
# File 'lib/rspec/mocks/proxy.rb', line 68

def remove_stub(method_name)
  method_double[method_name].remove_stub
end

#resetObject



78
79
80
# File 'lib/rspec/mocks/proxy.rb', line 78

def reset
  method_doubles.each {|d| d.reset}
end

#verifyObject

:nodoc:



72
73
74
75
76
# File 'lib/rspec/mocks/proxy.rb', line 72

def verify #:nodoc:
  method_doubles.each {|d| d.verify}
ensure
  reset
end