Object
######################################################################### 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).
The following methods are added to partial mocks so that they can act like a mock.
Initialize a PartialMockProxy object.
# 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
# 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
# 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.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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(...)
# 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
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.
# 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
Create an alias for the existing method_name. Returns the new alias name. If the aliasing process fails (because the method doesn’t really exist, then return nil.
# File lib/flexmock/partial_mock.rb, line 225 225: def create_alias_for_existing_method(method_name) 226: begin 227: new_alias = new_name(method_name) 228: unless @obj.respond_to?(new_alias) 229: sclass.class_eval do 230: alias_method(new_alias, method_name) 231: end 232: end 233: new_alias 234: rescue NameError => ex 235: # Alias attempt failed 236: nil 237: end 238: end
Define a proxy method that forwards to our mock object. The proxy method is defined as a singleton method on the object being mocked.
# File lib/flexmock/partial_mock.rb, line 243 243: def define_proxy_method(method_name) 244: if method_name.to_s =~ /=$/ 245: eval_line = __LINE__ + 1 246: sclass.class_eval %{ 247: def #{method_name}(*args, &block) 248: @flexmock_proxy.mock.__send__(:#{method_name}, *args, &block) 249: end 250: }, __FILE__, eval_line 251: else 252: eval_line = __LINE__ + 1 253: sclass.class_eval %{ 254: def #{method_name}(*args, &block) 255: @flexmock_proxy.mock.#{method_name}(*args, &block) 256: end 257: }, __FILE__, eval_line 258: make_rcov_recognize_the_above_eval_is_covered = true 259: end 260: end
Have we been detached from the existing object?
# File lib/flexmock/partial_mock.rb, line 281 281: def detached? 282: @obj.nil? 283: end
Hide the existing method definition with a singleton defintion that proxies to our mock object. If the current definition is a singleton, we need to record the definition and remove it before creating our own singleton method. If the current definition is not a singleton, all we need to do is override it with our own singleton.
# File lib/flexmock/partial_mock.rb, line 198 198: def hide_existing_method(method_name) 199: stow_existing_definition(method_name) 200: define_proxy_method(method_name) 201: end
Invoke the original definition of method on the object supported by the stub.
# File lib/flexmock/partial_mock.rb, line 140 140: def invoke_original(method, args) 141: method_proc = @method_definitions[method] 142: method_proc.call(*args) 143: end
Generate a name to be used to alias the original behavior.
# File lib/flexmock/partial_mock.rb, line 286 286: def new_name(old_name) 287: "flexmock_original_behavior_for_#{old_name}" 288: end
Remove the current method if it is a singleton method of the object being mocked.
# File lib/flexmock/partial_mock.rb, line 276 276: def remove_current_method(method_name) 277: sclass.class_eval { remove_method(method_name) } 278: end
Restore the original singleton defintion for method_name that was saved earlier.
# File lib/flexmock/partial_mock.rb, line 264 264: def restore_original_definition(method_name) 265: method_def = @method_definitions[method_name] 266: if method_def 267: the_alias = new_name(method_name) 268: sclass.class_eval do 269: alias_method(method_name, the_alias) 270: end 271: end 272: end
The singleton class of the object.
# File lib/flexmock/partial_mock.rb, line 182 182: def sclass 183: class << @obj; self; end 184: end
Is the given method name a singleton method in the object we are mocking?
# File lib/flexmock/partial_mock.rb, line 188 188: def singleton?(method_name) 189: @obj.methods(false).include?(method_name.to_s) 190: end
Stow the existing method definition so that it can be recovered later.
# File lib/flexmock/partial_mock.rb, line 205 205: def stow_existing_definition(method_name) 206: @methods_proxied << method_name 207: new_alias = create_alias_for_existing_method(method_name) 208: if new_alias 209: my_object = @obj 210: @method_definitions[method_name] = Proc.new { |*args| 211: block = nil 212: if Proc === args.last 213: block = args.last 214: args = args[0...1] 215: end 216: my_object.send(new_alias, *args, &block) 217: } 218: end 219: remove_current_method(method_name) if singleton?(method_name) 220: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.