Class: RSpec::Mocks::Proxy

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

Direct Known Subclasses

PartialDoubleProxy, TestDoubleProxy, VerifyingProxy

Defined Under Namespace

Classes: SpecificMessage

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Proxy) initialize(object, order_group, name = nil, options = {})

Returns a new instance of Proxy



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

def initialize(object, order_group, name=nil, options={})
  @object = object
  @order_group = order_group
  @name = name
  @error_generator = ErrorGenerator.new(object, name)
  @messages_received = []
  @options = options
  @null_object = false
  @method_doubles = Hash.new { |h, k| h[k] = MethodDouble.new(@object, k, self) }
end

Instance Attribute Details

- (Object) object (readonly)



24
25
26
# File 'lib/rspec/mocks/proxy.rb', line 24

def object
  @object
end

Instance Method Details

- (Object) add_message_expectation(method_name, opts = {}, &block)



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rspec/mocks/proxy.rb', line 45

def add_message_expectation(method_name, opts={}, &block)
  location = opts.fetch(:expected_from) { CallerFilter.first_non_rspec_line }
  meth_double = method_double_for(method_name)

  if null_object? && !block
    meth_double.add_default_stub(@error_generator, @order_group, location, opts) do
      @object
    end
  end

  meth_double.add_expectation @error_generator, @order_group, location, opts, &block
end

- (Object) add_simple_expectation(method_name, response, location)



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

def add_simple_expectation(method_name, response, location)
  method_double_for(method_name).add_simple_expectation method_name, response, @error_generator, location
end

- (Object) add_simple_stub(method_name, response)



111
112
113
# File 'lib/rspec/mocks/proxy.rb', line 111

def add_simple_stub(method_name, response)
  method_double_for(method_name).add_simple_stub method_name, response
end

- (Object) add_stub(method_name, opts = {}, &implementation)



105
106
107
108
# File 'lib/rspec/mocks/proxy.rb', line 105

def add_stub(method_name, opts={}, &implementation)
  location = opts.fetch(:expected_from) { CallerFilter.first_non_rspec_line }
  method_double_for(method_name).add_stub @error_generator, @order_group, location, opts, &implementation
end

- (Object) as_null_object

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



34
35
36
37
# File 'lib/rspec/mocks/proxy.rb', line 34

def as_null_object
  @null_object = true
  @object
end

- (Object) build_expectation(method_name)



64
65
66
67
68
69
70
71
# File 'lib/rspec/mocks/proxy.rb', line 64

def build_expectation(method_name)
  meth_double = method_double_for(method_name)

  meth_double.build_expectation(
    @error_generator,
    @order_group
  )
end

- (Object) check_for_unexpected_arguments(expectation)



96
97
98
99
100
101
102
# File 'lib/rspec/mocks/proxy.rb', line 96

def check_for_unexpected_arguments(expectation)
  @messages_received.each do |(method_name, args, _)|
    if expectation.matches_name_but_not_args(method_name, *args)
      raise_unexpected_message_args_error(expectation, *args)
    end
  end
end

- (Boolean) has_negative_expectation?(message)

Returns:

  • (Boolean)


141
142
143
# File 'lib/rspec/mocks/proxy.rb', line 141

def has_negative_expectation?(message)
  method_double_for(message).expectations.detect {|expectation| expectation.negative_expectation_for?(message)}
end

- (Object) message_received(message, *args, &block)



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rspec/mocks/proxy.rb', line 152

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

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

- (Boolean) null_object?

Returns:

  • (Boolean)


27
28
29
# File 'lib/rspec/mocks/proxy.rb', line 27

def null_object?
  @null_object
end

- (Object) original_method_handle_for(message)



40
41
42
# File 'lib/rspec/mocks/proxy.rb', line 40

def original_method_handle_for(message)
  nil
end

- (Object) prepended_modules_of_singleton_class



200
201
202
203
204
205
206
207
# File 'lib/rspec/mocks/proxy.rb', line 200

def prepended_modules_of_singleton_class
  @prepended_modules_of_singleton_class ||= begin
    singleton_class = @object.singleton_class
    singleton_class.ancestors.take_while do |mod|
      !(Class === mod)
    end
  end
end

- (Object) raise_missing_default_stub_error(expectation, *args)



189
190
191
# File 'lib/rspec/mocks/proxy.rb', line 189

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

- (Object) raise_unexpected_message_args_error(expectation, *args)



184
185
186
# File 'lib/rspec/mocks/proxy.rb', line 184

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

- (Object) raise_unexpected_message_error(method_name, *args)



179
180
181
# File 'lib/rspec/mocks/proxy.rb', line 179

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

- (Boolean) received_message?(method_name, *args, &block)

Returns:

  • (Boolean)


136
137
138
# File 'lib/rspec/mocks/proxy.rb', line 136

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

- (Object) record_message_received(message, *args, &block)



146
147
148
149
# File 'lib/rspec/mocks/proxy.rb', line 146

def record_message_received(message, *args, &block)
  @order_group.invoked SpecificMessage.new(object, message)
  @messages_received << [message, args, block]
end

- (Object) remove_stub(method_name)



116
117
118
# File 'lib/rspec/mocks/proxy.rb', line 116

def remove_stub(method_name)
  method_double_for(method_name).remove_stub
end

- (Object) remove_stub_if_present(method_name)



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

def remove_stub_if_present(method_name)
  method_double_for(method_name).remove_stub_if_present
end

- (Object) replay_received_message_on(expectation, &block)



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rspec/mocks/proxy.rb', line 74

def replay_received_message_on(expectation, &block)
  expected_method_name = expectation.message
  meth_double = method_double_for(expected_method_name)

  if meth_double.expectations.any?
    @error_generator.raise_expectation_on_mocked_method(expected_method_name)
  end

  unless null_object? || meth_double.stubs.any?
    @error_generator.raise_expectation_on_unstubbed_method(expected_method_name)
  end

  @messages_received.each do |(actual_method_name, args, _)|
    if expectation.matches?(actual_method_name, *args)
      expectation.invoke(nil)
      block.call(*args) if block
    end
  end

end

- (Object) reset



131
132
133
# File 'lib/rspec/mocks/proxy.rb', line 131

def reset
  @messages_received.clear
end

- (Object) verify



126
127
128
# File 'lib/rspec/mocks/proxy.rb', line 126

def verify
  @method_doubles.each_value {|d| d.verify}
end

- (Object) visibility_for(method_name)



194
195
196
197
# File 'lib/rspec/mocks/proxy.rb', line 194

def visibility_for(method_name)
  # This is the default (for test doubles). Subclasses override this.
  :public
end