Module: Mocha::ObjectMethods
- Included in:
- Object
- Defined in:
- lib/mocha/object.rb,
lib/mocha/inspect.rb,
lib/mocha/metaclass.rb,
lib/mocha/parameter_matchers/object.rb
Overview
Methods added all objects to allow mocking and stubbing on real objects.
Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
Instance Method Summary collapse
- #__metaclass__ ⇒ Object
-
#expects(method_name_or_hash) ⇒ Object
:call-seq: expects(method_name) -> expectation expects(method_names_vs_return_values) -> last expectation.
-
#method_exists?(method, include_public_methods = true) ⇒ Boolean
:nodoc:.
-
#mocha ⇒ Object
:nodoc:.
- #mocha_inspect ⇒ Object
-
#reset_mocha ⇒ Object
:nodoc:.
-
#stubba_method ⇒ Object
:nodoc:.
-
#stubba_object ⇒ Object
:nodoc:.
-
#stubs(method_name_or_hash) ⇒ Object
:call-seq: stubs(method_name) -> expectation stubs(method_names_vs_return_values) -> last expectation.
-
#to_matcher ⇒ Object
:nodoc:.
-
#unstub(*method_names) ⇒ Object
:call-seq: unstub(*method_names).
Instance Method Details
#__metaclass__ ⇒ Object
4 5 6 |
# File 'lib/mocha/metaclass.rb', line 4 def class << self; self; end end |
#expects(method_name_or_hash) ⇒ Object
:call-seq: expects(method_name) -> expectation
expects(method_names_vs_return_values) -> last expectation
Adds an expectation that a method identified by method_name Symbol must be called exactly once with any parameters. Returns the new expectation which can be further modified by methods on Mocha::Expectation.
product = Product.new
product.expects(:save).returns(true)
assert_equal true, product.save
The original implementation of Product#save is replaced temporarily.
The original implementation of Product#save is restored at the end of the test.
If method_names_vs_return_values is a Hash, an expectation will be set up for each entry using the key as method_name and value as return_value.
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)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mocha/object.rb', line 53 def expects(method_name_or_hash) expectation = nil mockery = Mocha::Mockery.instance iterator = ArgumentIterator.new(method_name_or_hash) iterator.each { |*args| method_name = args.shift mockery.on_stubbing(self, method_name) method = stubba_method.new(stubba_object, method_name) mockery.stubba.stub(method) expectation = mocha.expects(method_name, caller) expectation.returns(args.shift) if args.length > 0 } expectation end |
#method_exists?(method, include_public_methods = true) ⇒ Boolean
:nodoc:
138 139 140 141 142 143 144 145 146 |
# File 'lib/mocha/object.rb', line 138 def method_exists?(method, include_public_methods = true) # :nodoc: if include_public_methods return true if public_methods(include_superclass_methods = true).include?(method) return true if respond_to?(method.to_sym) end return true if protected_methods(include_superclass_methods = true).include?(method) return true if private_methods(include_superclass_methods = true).include?(method) return false end |
#mocha ⇒ Object
:nodoc:
15 16 17 |
# File 'lib/mocha/object.rb', line 15 def mocha # :nodoc: @mocha ||= Mocha::Mockery.instance.mock_impersonating(self) end |
#mocha_inspect ⇒ Object
6 7 8 9 10 |
# File 'lib/mocha/inspect.rb', line 6 def mocha_inspect address = self.__id__ * 2 address += 0x100000000 if address < 0 inspect =~ /#</ ? "#<#{self.class}:0x#{'%x' % address}>" : inspect end |
#reset_mocha ⇒ Object
:nodoc:
19 20 21 |
# File 'lib/mocha/object.rb', line 19 def reset_mocha # :nodoc: @mocha = nil end |
#stubba_method ⇒ Object
:nodoc:
23 24 25 |
# File 'lib/mocha/object.rb', line 23 def stubba_method # :nodoc: Mocha::InstanceMethod end |
#stubba_object ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/mocha/object.rb', line 27 def stubba_object # :nodoc: self end |
#stubs(method_name_or_hash) ⇒ Object
:call-seq: stubs(method_name) -> expectation
stubs(method_names_vs_return_values) -> last expectation
Adds an expectation that a method identified by method_name Symbol may be called any number of times with any parameters. Returns the new expectation which can be further modified by methods on Mocha::Expectation.
product = Product.new
product.stubs(:save).returns(true)
assert_equal true, product.save
The original implementation of Product#save is replaced temporarily.
The original implementation of Product#save is restored at the end of the test.
If method_names_vs_return_values is a Hash, an expectation will be set up for each entry using the key as method_name and value as return_value.
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)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mocha/object.rb', line 90 def stubs(method_name_or_hash) expectation = nil mockery = Mocha::Mockery.instance iterator = ArgumentIterator.new(method_name_or_hash) iterator.each { |*args| method_name = args.shift mockery.on_stubbing(self, method_name) method = stubba_method.new(stubba_object, method_name) mockery.stubba.stub(method) expectation = mocha.stubs(method_name, caller) expectation.returns(args.shift) if args.length > 0 } expectation end |
#to_matcher ⇒ Object
:nodoc:
6 7 8 |
# File 'lib/mocha/parameter_matchers/object.rb', line 6 def to_matcher # :nodoc: Mocha::ParameterMatchers::Equals.new(self) end |
#unstub(*method_names) ⇒ Object
:call-seq: unstub(*method_names)
Removes the method stub added by calls to #expects or #stubs. Restores the original behaviour of the method before it was stubbed.
multiplier = Multiplier.new
multiplier.double(2) # => 4
multiplier.stubs(:double).raises
multiplier.double(2) # => raises exception
multiplier.unstubs(:double)
multiplier.double(2) # => 4
The original implementation of Multiplier#double is replaced temporarily.
The original implementation of Multiplier#double is restored when #unstub is called.
WARNING: If you #unstub a method which still has unsatisfied expectations, you may be removing the only way those expectations can be satisfied. Use #unstub with care.
If multiple method_names are supplied, each method is unstubbed.
multiplier.unstub(:double, :triple)
# exactly equivalent to
multiplier.unstub(:double)
multiplier.unstub(:triple)
130 131 132 133 134 135 136 |
# File 'lib/mocha/object.rb', line 130 def unstub(*method_names) mockery = Mocha::Mockery.instance method_names.each do |method_name| method = stubba_method.new(stubba_object, method_name) mockery.stubba.unstub(method) end end |