Class | Object |
In: |
lib/mocha/inspect.rb
lib/mocha/is_a.rb lib/mocha/metaclass.rb lib/mocha/object.rb lib/mocha/parameter_matchers/object.rb |
Parent: | Object |
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.
Adds an expectation that a method identified by 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 false, 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.
# File lib/mocha/object.rb, line 39 39: def expects(symbol) 40: mockery = Mocha::Mockery.instance 41: mockery.on_stubbing(self, symbol) 42: method = stubba_method.new(stubba_object, symbol) 43: mockery.stubba.stub(method) 44: mocha.expects(symbol, caller) 45: end
# File lib/mocha/object.rb, line 66 66: def method_exists?(symbol, include_public_methods = true) 67: existing_methods = private_methods(include_superclass_methods = true) + protected_methods(include_superclass_methods = true) 68: existing_methods += public_methods(include_superclass_methods = true) if include_public_methods 69: existing_methods.any? { |m| m.to_s == symbol.to_s } || (respond_to?(symbol) && include_public_methods) 70: end
# File lib/mocha/inspect.rb, line 4 4: def mocha_inspect 5: address = self.__id__ * 2 6: address += 0x100000000 if address < 0 7: inspect =~ /#</ ? "#<#{self.class}:0x#{'%x' % address}>" : inspect 8: end
Adds an expectation that a method identified by 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 false, 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.
# File lib/mocha/object.rb, line 58 58: def stubs(symbol) 59: mockery = Mocha::Mockery.instance 60: mockery.on_stubbing(self, symbol) 61: method = stubba_method.new(stubba_object, symbol) 62: mockery.stubba.stub(method) 63: mocha.stubs(symbol, caller) 64: end