######################################################################### 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).
- add_mock_method
- any_instance
- flexmock_container
- flexmock_container=
- flexmock_expectations_for
- flexmock_get
- flexmock_teardown
- flexmock_verify
- new
- new_instances
- should_receive
| MOCK_METHODS | = | [ :should_receive, :new_instances, :flexmock_get, :flexmock_teardown, :flexmock_verify |
| The following methods are added to partial mocks so that they can act like a mock. | ||
| [R] | mock |
Initialize a PartialMockProxy object.
[ show source ]
# File lib/flexmock/partial_mock.rb, line 40
40: def initialize(obj, mock, safe_mode)
41: @obj = obj
42: @mock = mock
43: @method_definitions = {}
44: @methods_proxied = []
45: unless safe_mode
46: add_mock_method(@obj, :should_receive)
47: MOCK_METHODS.each do |sym|
48: unless @obj.respond_to?(sym)
49: add_mock_method(@obj, sym)
50: end
51: end
52: end
53: end
[ show source ]
# File lib/flexmock/partial_mock.rb, line 91
91: def add_mock_method(obj, method_name)
92: stow_existing_definition(method_name)
93: eval_line = __LINE__ + 1
94: eval %{
95: def obj.#{method_name}(*args, &block)
96: @flexmock_proxy.#{method_name}(*args, &block)
97: end
98: }, binding, __FILE__, eval_line
99: end
any_instance is present for backwards compatibility with version 0.5.0. @deprecated
[ show source ]
# File lib/flexmock/deprecated_methods.rb, line 53
53: def any_instance(&block)
54: $stderr.puts "any_instance is deprecated, use new_instances instead."
55: new_instances(&block)
56: end
Forward to the mock‘s container.
[ show source ]
# File lib/flexmock/partial_mock.rb, line 165
165: def flexmock_container
166: @mock.flexmock_container
167: end
Set the proxy‘s mock container. This set value is ignored because the proxy always uses the container of its mock.
[ show source ]
# File lib/flexmock/partial_mock.rb, line 171
171: def flexmock_container=(container)
172: end
Forward the request for the expectation director to the mock.
[ show source ]
# File lib/flexmock/partial_mock.rb, line 175
175: def flexmock_expectations_for(method_name)
176: @mock.flexmock_expectations_for(method_name)
177: end
Get the mock object for the partial mock.
[ show source ]
# File lib/flexmock/partial_mock.rb, line 56
56: def flexmock_get
57: @mock
58: end
Remove all traces of the mocking framework from the existing object.
[ show source ]
# File lib/flexmock/partial_mock.rb, line 153
153: def flexmock_teardown
154: if ! detached?
155: @methods_proxied.each do |method_name|
156: remove_current_method(method_name)
157: restore_original_definition(method_name)
158: end
159: @obj.instance_variable_set("@flexmock_proxy", nil)
160: @obj = nil
161: end
162: end
Verify that the mock has been properly called. After verification, detach the mocking infrastructure from the existing object.
[ show source ]
# File lib/flexmock/partial_mock.rb, line 148
148: def flexmock_verify
149: @mock.flexmock_verify
150: end
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(...)
[ show source ]
# File lib/flexmock/partial_mock.rb, line 119
119: def new_instances(*allocators, &block)
120: fail ArgumentError, "new_instances requires a Class to stub" unless Class === @obj
121: allocators = [:new] if allocators.empty?
122: result = ExpectationRecorder.new
123: allocators.each do |allocate_method|
124: # HACK: Without the following lambda, Ruby 1.9 will not bind
125: # the allocate_method parameter correctly.
126: lambda { }
127: self.should_receive(allocate_method).and_return { |*args|
128: new_obj = invoke_original(allocate_method, args)
129: mock = flexmock_container.flexmock(new_obj)
130: block.call(mock) if block_given?
131: result.apply(mock)
132: new_obj
133: }
134: end
135: result
136: end
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.
[ show source ]
# File lib/flexmock/partial_mock.rb, line 80
80: def should_receive(*args)
81: ContainerHelper.parse_should_args(@mock, args) do |sym|
82: unless @methods_proxied.include?(sym)
83: hide_existing_method(sym)
84: end
85: ex = @mock.should_receive(sym)
86: ex.mock = self
87: ex
88: end
89: end