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_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).
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(:should_receive) 47: MOCK_METHODS.each do |sym| 48: unless @obj.respond_to?(sym) 49: add_mock_method(sym) 50: end 51: end 52: end 53: end
# File lib/flexmock/partial_mock.rb, line 91 91: def add_mock_method(method_name) 92: stow_existing_definition(method_name) 93: sclass.module_eval do 94: define_method(method_name) { |*args, &block| 95: proxy = instance_variable_get("@flexmock_proxy") 96: if proxy.nil? 97: fail "Missing FlexMock proxy " + 98: "(for method_name=#{method_name.inspect}, self=\#{self})" 99: end 100: proxy.send(method_name, *args, &block) 101: } 102: end 103: 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 170 170: def flexmock_container 171: @mock.flexmock_container 172: 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 176 176: def flexmock_container=(container) 177: end
Forward the request for the expectation director to the mock.
# File lib/flexmock/partial_mock.rb, line 180 180: def flexmock_expectations_for(method_name) 181: @mock.flexmock_expectations_for(method_name) 182: 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 158 158: def flexmock_teardown 159: if ! detached? 160: @methods_proxied.each do |method_name| 161: remove_current_method(method_name) 162: restore_original_definition(method_name) 163: end 164: @obj.instance_variable_set("@flexmock_proxy", nil) 165: @obj = nil 166: end 167: 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 153 153: def flexmock_verify 154: @mock.flexmock_verify 155: 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 123 123: def new_instances(*allocators, &block) 124: fail ArgumentError, "new_instances requires a Class to stub" unless Class === @obj 125: allocators = [:new] if allocators.empty? 126: result = ExpectationRecorder.new 127: allocators.each do |allocate_method| 128: check_allocate_method(allocate_method) 129: # HACK: Without the following lambda, Ruby 1.9 will not bind 130: # the allocate_method parameter correctly. 131: lambda { } 132: self.should_receive(allocate_method).and_return { |*args| 133: new_obj = invoke_original(allocate_method, args) 134: mock = flexmock_container.flexmock(new_obj) 135: block.call(mock) if block_given? 136: result.apply(mock) 137: new_obj 138: } 139: end 140: result 141: 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
# File lib/flexmock/partial_mock.rb, line 186 186: def check_allocate_method(allocate_method) 187: if allocate_method == :allocate && RUBY_VERSION >= "1.9" 188: fail UsageError, "Cannot mock the allocation method using new_instances in Ruby 1.9" 189: end 190: end
Invoke the original definition of method on the object supported by the stub.
# File lib/flexmock/partial_mock.rb, line 145 145: def invoke_original(method, args) 146: method_proc = @method_definitions[method] 147: method_proc.call(*args) 148: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.