Methods added to all objects to allow mocking and stubbing on real (i.e. non-mock) objects.
Both {#} and {#} return an {Expectation} which can be further modified by methods on {Expectation}.
Adds an expectation that the specified method must be called exactly once with any parameters.
The original implementation of the method is replaced during the test and then restored at the end of the test.
@param [Symbol,String] method_name name of expected method @param [Hash] expected_methods_vs_return_values expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {#} were called multiple times.
@overload def expects(method_name) @overload def expects(expected_methods_vs_return_values) @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}. @raise [StubbingError] if attempting to stub method which is not allowed.
@example Setting up an expectation on a non-mock object.
product = Product.new product.expects(:save).returns(true) assert_equal true, product.save
@example Setting up multiple expectations on a non-mock object.
product = Product.new product.expects(:valid? => true, :save => true) # exactly equivalent to product = Product.new product.expects(:valid?).returns(true) product.expects(:save).returns(true)
@see Mock#expects
# File lib/mocha/object.rb, line 66 66: def expects(expected_methods_vs_return_values) 67: if expected_methods_vs_return_values.to_s =~ /the[^a-z]*spanish[^a-z]*inquisition/ 68: raise Mocha::ExpectationError.new('NOBODY EXPECTS THE SPANISH INQUISITION!') 69: end 70: if frozen? 71: raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller) 72: end 73: expectation = nil 74: mockery = Mocha::Mockery.instance 75: iterator = ArgumentIterator.new(expected_methods_vs_return_values) 76: iterator.each { |*args| 77: method_name = args.shift 78: mockery.on_stubbing(self, method_name) 79: method = stubba_method.new(stubba_object, method_name) 80: mockery.stubba.stub(method) 81: expectation = mocha.expects(method_name, caller) 82: expectation.returns(args.shift) if args.length > 0 83: } 84: expectation 85: end
@private
# File lib/mocha/object.rb, line 163 163: def method_exists?(method, include_public_methods = true) 164: if include_public_methods 165: return true if public_methods(include_superclass_methods = true).include?(method) 166: return true if respond_to?(method.to_sym) 167: end 168: return true if protected_methods(include_superclass_methods = true).include?(method) 169: return true if private_methods(include_superclass_methods = true).include?(method) 170: return false 171: end
@private
# File lib/mocha/object.rb, line 19 19: def mocha 20: @mocha ||= Mocha::Mockery.instance.mock_impersonating(self) 21: end
# File lib/mocha/inspect.rb, line 6 6: def mocha_inspect 7: address = self.__id__ * 2 8: address += 0x100000000 if address < 0 9: inspect =~ /#</ ? "#<#{self.class}:0x#{'%x' % address}>" : inspect 10: end
@private
# File lib/mocha/object.rb, line 24 24: def reset_mocha 25: @mocha = nil 26: end
@private
# File lib/mocha/object.rb, line 29 29: def stubba_method 30: Mocha::InstanceMethod 31: end
@private
# File lib/mocha/object.rb, line 34 34: def stubba_object 35: self 36: end
Adds an expectation that the specified method may be called any number of times with any parameters.
@param [Symbol,String] method_name name of stubbed method @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {#} were called multiple times.
@overload def stubs(method_name) @overload def stubs(stubbed_methods_vs_return_values) @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}. @raise [StubbingError] if attempting to stub method which is not allowed.
@example Setting up a stubbed methods on a non-mock object.
product = Product.new product.stubs(:save).returns(true) assert_equal true, product.save
@example Setting up multiple stubbed methods on a non-mock object.
product = Product.new product.stubs(:valid? => true, :save => true) # exactly equivalent to product = Product.new product.stubs(:valid?).returns(true) product.stubs(:save).returns(true)
@see Mock#stubs
# File lib/mocha/object.rb, line 113 113: def stubs(stubbed_methods_vs_return_values) 114: if frozen? 115: raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller) 116: end 117: expectation = nil 118: mockery = Mocha::Mockery.instance 119: iterator = ArgumentIterator.new(stubbed_methods_vs_return_values) 120: iterator.each { |*args| 121: method_name = args.shift 122: mockery.on_stubbing(self, method_name) 123: method = stubba_method.new(stubba_object, method_name) 124: mockery.stubba.stub(method) 125: expectation = mocha.stubs(method_name, caller) 126: expectation.returns(args.shift) if args.length > 0 127: } 128: expectation 129: end
@private
# File lib/mocha/parameter_matchers/object.rb, line 7 7: def to_matcher 8: Mocha::ParameterMatchers::Equals.new(self) 9: end
Removes the specified stubbed methods (added by calls to {#} or {#}) and all expectations associated with them.
Restores the original behaviour of the methods before they were stubbed.
WARNING: If you {#} a method which still has unsatisfied expectations, you may be removing the only way those expectations can be satisfied. Use {#} with care.
@param [Array
@example Stubbing and unstubbing a method on a real (non-mock) object.
multiplier = Multiplier.new multiplier.double(2) # => 4 multiplier.stubs(:double).raises # new behaviour defined multiplier.double(2) # => raises exception multiplier.unstub(:double) # original behaviour restored multiplier.double(2) # => 4
@example Unstubbing multiple methods on a real (non-mock) object.
multiplier.unstub(:double, :triple) # exactly equivalent to multiplier.unstub(:double) multiplier.unstub(:triple)
# File lib/mocha/object.rb, line 154 154: def unstub(*method_names) 155: mockery = Mocha::Mockery.instance 156: method_names.each do |method_name| 157: method = stubba_method.new(stubba_object, method_name) 158: mockery.stubba.unstub(method) 159: end 160: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.