Class: FlexMock::PartialMockProxy

Inherits:
Object
  • Object
show all
Includes:
Ordering
Defined in:
lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb,
lib/gems/flexmock-0.8.3/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_mock). 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, 
  :flexmock_get,   :flexmock_teardown, :flexmock_verify
]

Instance Attribute 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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb', line 40

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

Instance Attribute Details

#mockObject (readonly)

Returns the value of attribute mock.



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

def mock
  @mock
end

Instance Method Details

#add_mock_method(obj, method_name) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb', line 91

def add_mock_method(obj, method_name)
  stow_existing_definition(method_name)
  eval_line = __LINE__ + 1
  eval %{
    def obj.#{method_name}(*args, &block)
      @flexmock_proxy.#{method_name}(*args, &block)
    end
  }, binding, __FILE__, eval_line
end

#any_instance(&block) ⇒ Object

Deprecated.

any_instance is present for backwards compatibility with version 0.5.0.



53
54
55
56
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/deprecated_methods.rb', line 53

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

#flexmock_containerObject

Forward to the mock’s container.



165
166
167
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb', line 165

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.



171
172
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb', line 171

def flexmock_container=(container)
end

#flexmock_expectations_for(method_name) ⇒ Object

Forward the request for the expectation director to the mock.



175
176
177
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb', line 175

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

#flexmock_getObject

Get the mock object for the partial mock.



56
57
58
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb', line 56

def flexmock_get
  @mock
end

#flexmock_teardownObject

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



153
154
155
156
157
158
159
160
161
162
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb', line 153

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.



148
149
150
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb', line 148

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(...)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb', line 119

def new_instances(*allocators, &block)
  fail ArgumentError, "new_instances requires a Class to stub" unless Class === @obj
  allocators = [:new] if allocators.empty?
  result = ExpectationRecorder.new
  allocators.each do |allocate_method|
    # HACK: Without the following lambda, Ruby 1.9 will not bind
    # the allocate_method parameter correctly.
    lambda { } 
    self.should_receive(allocate_method).and_return { |*args|
      new_obj = invoke_original(allocate_method, args)
      mock = flexmock_container.flexmock(new_obj)
      block.call(mock) if block_given?
      result.apply(mock)
      new_obj
    }
  end
  result
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.



80
81
82
83
84
85
86
87
88
89
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb', line 80

def should_receive(*args)
  ContainerHelper.parse_should_args(@mock, args) do |sym|
    unless @methods_proxied.include?(sym)
      hide_existing_method(sym)
    end
    ex = @mock.should_receive(sym)
    ex.mock = self
    ex
  end
end