Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/object_mock.rb
Overview
A group of methods added to Object class to enable simple mocking of instance or class methods. This mocking can be done temporarily within the scope of a block. Alternatively it can be done indefinitely until explicitly undone.
Instance Method Summary collapse
-
#class_mock(methods, &block) ⇒ Object
The same as mock methods but applies mocking for all instances of the class This method can be either called on the class object or on any instance of the class.
-
#class_unmock(*methods) ⇒ Object
method for unmocking methods mocked using class_mock This method can be either called on the class object or on any instance of the class methods: a comma separated list of names of instance methods to be unmocked when no methods are passed all mocked instance methods will be unmocked returns: array of the names of methods unmocked.
-
#mock(methods) ⇒ Object
method for mocking or adding methods for a single object (or class methods if the object is a class instance) methods: a comma separated key value pairs where the key is the name of the instance method to be mocked and the value is the new definition in the form of a proc or lambda or simply the required return for the mocked method An optional block can passed so that the mocking is applied only within the scope of the block.
-
#unmock(*methods) ⇒ Object
method for unmocking methods mocked by mock method methods: a comma separated list of names of instance methods to be unmocked when no methods are passed all mocked instance methods will be unmocked returns: array of the names of methods unmocked.
Instance Method Details
#class_mock(methods, &block) ⇒ Object
The same as mock methods but applies mocking for all instances of the class This method can be either called on the class object or on any instance of the class
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/object_mock.rb', line 37 def class_mock(methods, &block) return self.class.class_mock(methods, &block) if !self.is_a?(Module) do_mock(methods, :instance, lambda {|m| instance_methods.collect{|n| n.to_sym}.include?(m.to_sym)}, self) if block begin block.call ensure class_unmock(*methods.keys) end end end |
#class_unmock(*methods) ⇒ Object
method for unmocking methods mocked using class_mock This method can be either called on the class object or on any instance of the class methods: a comma separated list of names of instance methods to be unmocked when no methods are passed all mocked instance methods will be unmocked returns: array of the names of methods unmocked
56 57 58 59 |
# File 'lib/object_mock.rb', line 56 def class_unmock(*methods) return self.class.class_unmock(*methods) if !self.is_a?(Module) do_unmock(methods, :instance, self) end |
#mock(methods) ⇒ Object
method for mocking or adding methods for a single object (or class methods if the object is a class instance) methods: a comma separated key value pairs where the key is the name of the instance method to be mocked and the value is the new definition in the form of a proc or lambda or simply the required return for the mocked method An optional block can passed so that the mocking is applied only within the scope of the block
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/object_mock.rb', line 16 def mock(methods) do_mock(methods) if block_given? begin yield ensure unmock(*methods.keys) end end end |
#unmock(*methods) ⇒ Object
method for unmocking methods mocked by mock method methods: a comma separated list of names of instance methods to be unmocked when no methods are passed all mocked instance methods will be unmocked returns: array of the names of methods unmocked
31 32 33 |
# File 'lib/object_mock.rb', line 31 def unmock(*methods) do_unmock(methods) end |