Class: FlexMock::PartialMockProxy

Inherits:
Object
  • Object
show all
Includes:
Ordering
Defined in:
lib/flexmock/partial_mock.rb,
lib/flexmock/deprecated_methods.rb

Overview

PartialMockProxy is used to mate the mock framework to an existing object. The object is “enhanced” with a reference to a mock object (stored in @flexmock_proxy). When the should_receive method is sent to the proxy, it overrides the existing object’s method by creating singleton method that forwards to the mock. When testing is complete, PartialMockProxy will erase the mocking infrastructure from the object being mocked (e.g. remove instance variables and mock singleton methods).

Constant Summary collapse

MOCK_METHODS =

The following methods are added to partial mocks so that they can act like a mock.

[
  :should_receive, :new_instances,
  :should_receive_with_location,
  :flexmock_get,   :flexmock_teardown, :flexmock_verify,
  :flexmock_received?, :flexmock_calls, :flexmock_find_expectation
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ordering

#flexmock_allocate_order, #flexmock_current_order, #flexmock_current_order=, #flexmock_groups, #flexmock_validate_order

Constructor Details

#initialize(obj, mock, safe_mode) ⇒ PartialMockProxy

Initialize a PartialMockProxy object.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/flexmock/partial_mock.rb', line 59

def initialize(obj, mock, safe_mode)
  @obj = obj
  @mock = mock
  @method_definitions = {}
  @methods_proxied = []
  unless safe_mode
    add_mock_method(:should_receive)
    MOCK_METHODS.each do |sym|
      unless @obj.respond_to?(sym)
        add_mock_method(sym)
      end
    end
  end
end

Instance Attribute Details

#mockObject (readonly)

Returns the value of attribute mock.



29
30
31
# File 'lib/flexmock/partial_mock.rb', line 29

def mock
  @mock
end

Class Method Details

.make_proxy_for(obj, container, name, safe_mode) ⇒ Object

Make a partial mock proxy and install it on the target obj.



32
33
34
35
36
37
38
39
40
# File 'lib/flexmock/partial_mock.rb', line 32

def self.make_proxy_for(obj, container, name, safe_mode)
  name ||= "flexmock(#{obj.class.to_s})"
  if ! proxy_defined_on?(obj)
    mock = FlexMock.new(name, container)
    proxy = PartialMockProxy.new(obj, mock, safe_mode)
    obj.instance_variable_set("@flexmock_proxy", proxy)
  end
  obj.instance_variable_get("@flexmock_proxy")
end

.proxy_defined_on?(obj) ⇒ Boolean

Is there a mock proxy defined on the domain object?

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/flexmock/partial_mock.rb', line 43

def self.proxy_defined_on?(obj)
  obj.instance_variable_defined?("@flexmock_proxy") &&
    obj.instance_variable_get("@flexmock_proxy")
end

Instance Method Details

#add_mock_method(method_name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/flexmock/partial_mock.rb', line 119

def add_mock_method(method_name)
  stow_existing_definition(method_name)
  target_class_eval do
    define_method(method_name) { |*args, &block|
      proxy = instance_variable_get("@flexmock_proxy") or
        fail "Missing FlexMock proxy " +
             "(for method_name=#{method_name.inspect}, self=\#{self})"
      proxy.send(method_name, *args, &block)
    }
  end
end

#any_instance(&block) ⇒ Object

Deprecated.

any_instance is present for backwards compatibility with version 0.5.0.



54
55
56
57
# File 'lib/flexmock/deprecated_methods.rb', line 54

def any_instance(&block)
  $stderr.puts "any_instance is deprecated, use new_instances instead."
  new_instances(&block)
end

#flexmock_based_on(*args) ⇒ Object

Forward the based on request.



232
233
234
# File 'lib/flexmock/partial_mock.rb', line 232

def flexmock_based_on(*args)
  @mock.flexmock_based_on(*args)
end

#flexmock_callsObject

Forward to the mock



217
218
219
# File 'lib/flexmock/partial_mock.rb', line 217

def flexmock_calls
  @mock.flexmock_calls
end

#flexmock_containerObject

Forward to the mock’s container.



207
208
209
# File 'lib/flexmock/partial_mock.rb', line 207

def flexmock_container
  @mock.flexmock_container
end

#flexmock_container=(container) ⇒ Object

Set the proxy’s mock container. This set value is ignored because the proxy always uses the container of its mock.



223
224
# File 'lib/flexmock/partial_mock.rb', line 223

def flexmock_container=(container)
end

#flexmock_define_expectation(location, *args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/flexmock/partial_mock.rb', line 104

def flexmock_define_expectation(location, *args)
  EXP_BUILDER.parse_should_args(self, args) do |method_name|
    unless @methods_proxied.include?(method_name)
      hide_existing_method(method_name)
    end
    ex = @mock.flexmock_define_expectation(location, method_name)
    ex.mock = self
    ex
  end
end

#flexmock_expectations_for(method_name) ⇒ Object

Forward the request for the expectation director to the mock.



227
228
229
# File 'lib/flexmock/partial_mock.rb', line 227

def flexmock_expectations_for(method_name)
  @mock.flexmock_expectations_for(method_name)
end

#flexmock_find_expectation(*args) ⇒ Object



115
116
117
# File 'lib/flexmock/partial_mock.rb', line 115

def flexmock_find_expectation(*args)
  @mock.flexmock_find_expectation(*args)
end

#flexmock_getObject

Get the mock object for the partial mock.



75
76
77
# File 'lib/flexmock/partial_mock.rb', line 75

def flexmock_get
  @mock
end

#flexmock_invoke_original(method, args) ⇒ Object

Invoke the original definition of method on the object supported by the stub.



183
184
185
186
# File 'lib/flexmock/partial_mock.rb', line 183

def flexmock_invoke_original(method, args)
  method_proc = @method_definitions[method]
  method_proc.call(*args)
end

#flexmock_received?(*args) ⇒ Boolean

Forward to the mock

Returns:

  • (Boolean)


212
213
214
# File 'lib/flexmock/partial_mock.rb', line 212

def flexmock_received?(*args)
  @mock.flexmock_received?(*args)
end

#flexmock_teardownObject

Remove all traces of the mocking framework from the existing object.



195
196
197
198
199
200
201
202
203
204
# File 'lib/flexmock/partial_mock.rb', line 195

def flexmock_teardown
  if ! detached?
    @methods_proxied.each do |method_name|
      remove_current_method(method_name)
      restore_original_definition(method_name)
    end
    @obj.instance_variable_set("@flexmock_proxy", nil)
    @obj = nil
  end
end

#flexmock_verifyObject

Verify that the mock has been properly called. After verification, detach the mocking infrastructure from the existing object.



190
191
192
# File 'lib/flexmock/partial_mock.rb', line 190

def flexmock_verify
  @mock.flexmock_verify
end

#new_instances(*allocators, &block) ⇒ Object

:call-seq:

new_instances.should_receive(...)
new_instances { |instance|  instance.should_receive(...) }

new_instances is a short cut method for overriding the behavior of any new instances created via a mocked class object.

By default, new_instances will mock the behaviour of the :new method. If you wish to mock a different set of class methods, just pass a list of symbols to as arguments. (previous versions also mocked :allocate by default. If you need :allocate to be mocked, just request it explicitly).

For example, to stub only objects created by :make (and not :new), use:

flexmock(ClassName).new_instances(:make).should_receive(...)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/flexmock/partial_mock.rb', line 149

def new_instances(*allocators, &block)
  fail ArgumentError, "new_instances requires a Class to stub" unless
    Class === @obj
  location = caller.first
  allocators = [:new] if allocators.empty?
  expectation_recorder = ExpectationRecorder.new
  allocators.each do |allocate_method|
    check_allocate_method(allocate_method)
    flexmock_define_expectation(location, allocate_method).and_return { |*args|
      create_new_mocked_object(
        allocate_method, args, expectation_recorder, block)
    }
  end
  expectation_recorder
end

#should_receive(*args) ⇒ Object

:call-seq:

should_receive(:method_name)
should_receive(:method1, method2, ...)
should_receive(:meth1 => result1, :meth2 => result2, ...)

Declare that the partial mock should receive a message with the given name.

If more than one method name is given, then the mock object should expect to receive all the listed melthods. If a hash of method name/value pairs is given, then the each method will return the associated result. Any expectations applied to the result of should_receive will be applied to all the methods defined in the argument list.

An expectation object for the method name is returned as the result of this method. Further expectation constraints can be added by chaining to the result.

See Expectation for a list of declarators that can be used.



99
100
101
102
# File 'lib/flexmock/partial_mock.rb', line 99

def should_receive(*args)
  location = caller.first
  flexmock_define_expectation(location, *args)
end