Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/not_a_mock/object_extensions.rb
Class Method Summary collapse
-
.stub_instance(methods = {}) ⇒ Object
Called on a class, creates a stub instance of that class.
Instance Method Summary collapse
-
#meta_eval(&block) ⇒ Object
Evaluates the block in the context of this object’s metaclass.
-
#metaclass ⇒ Object
Returns the metaclass of this object.
-
#stub_method(method, &block) ⇒ Object
If passed a symbol and a block, this replaces the named method on this object with a stub version that evaluates the block and returns the result.
-
#stub_methods(methods) ⇒ Object
Takes a hash of method names mapped to results, and replaces each named method on this object with a stub version returning the corresponding result.
-
#stub_methods_to_raise(methods) ⇒ Object
(also: #stub_method_to_raise)
Takes a hash of method names mapped to exceptions, and replaces each named method on this object with a stub version returning the corresponding exception.
-
#track_methods(*methods) ⇒ Object
(also: #track_method, #log_calls_to)
Call this on any object or class with a list of method names.
-
#unstub_methods(*methods) ⇒ Object
(also: #unstub_method)
Removes the stubbed versions of the given methods and restores the original methods.
-
#untrack_methods(*methods) ⇒ Object
(also: #untrack_method)
Stop recording calls for the given methods.
Class Method Details
.stub_instance(methods = {}) ⇒ Object
Called on a class, creates a stub instance of that class. Takes a hash of method names and their returns values, and creates those methods on the new stub instance.
See NotAMock::Stub for more details about the returned objects.
89 90 91 |
# File 'lib/not_a_mock/object_extensions.rb', line 89 def stub_instance(methods = {}) NotAMock::Stub.new(self, methods) end |
Instance Method Details
#meta_eval(&block) ⇒ Object
Evaluates the block in the context of this object’s metaclass.
103 104 105 |
# File 'lib/not_a_mock/object_extensions.rb', line 103 def (&block) .instance_eval(&block) end |
#metaclass ⇒ Object
Returns the metaclass of this object. For an explanation of metaclasses, see: whytheluckystiff.net/articles/seeingMetaclassesClearly.html
96 97 98 99 100 |
# File 'lib/not_a_mock/object_extensions.rb', line 96 def class << self self end end |
#stub_method(method, &block) ⇒ Object
If passed a symbol and a block, this replaces the named method on this object with a stub version that evaluates the block and returns the result.
If passed a hash, this is an alias for stub_methods.
Calls to stubbed methods are recorded in the NotAMock::CallRecorder, so you can later make assertions about them as described in NotAMock::Matchers.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/not_a_mock/object_extensions.rb', line 36 def stub_method(method, &block) obj = self case method when Symbol NotAMock::CallRecorder.instance.untrack_method(obj, method) NotAMock::Stubber.instance.unstub_method(obj, method) stubber = NotAMock::Stubber.instance.stub_method(obj, method, &block) NotAMock::CallRecorder.instance.track_method(obj, method) when Hash stub_methods(method) else raise ArgumentError end stubber end |
#stub_methods(methods) ⇒ Object
Takes a hash of method names mapped to results, and replaces each named method on this object with a stub version returning the corresponding result.
Calls to stubbed methods are recorded in the NotAMock::CallRecorder, so you can later make assertions about them as described in NotAMock::Matchers.
58 59 60 61 62 |
# File 'lib/not_a_mock/object_extensions.rb', line 58 def stub_methods(methods) methods.each do |method, result| stub_method(method) {|*args| result } end end |
#stub_methods_to_raise(methods) ⇒ Object Also known as: stub_method_to_raise
Takes a hash of method names mapped to exceptions, and replaces each named method on this object with a stub version returning the corresponding exception.
66 67 68 69 70 |
# File 'lib/not_a_mock/object_extensions.rb', line 66 def stub_methods_to_raise(methods) methods.each do |method, exception| stub_method(method) {|*args| raise exception } end end |
#track_methods(*methods) ⇒ Object Also known as: track_method, log_calls_to
Call this on any object or class with a list of method names. Any future calls to those methods will be recorded in NotAMock::CallRecorder.
See NotAMock::Matchers for info on how to test which methods have been called, with what arguments, etc.
8 9 10 11 12 13 14 15 16 |
# File 'lib/not_a_mock/object_extensions.rb', line 8 def track_methods(*methods) if methods.empty? self.public_methods(false).map {|method_name| methods << method_name.to_s.to_sym} end methods.each do |method| NotAMock::CallRecorder.instance.track_method(self, method) end end |
#unstub_methods(*methods) ⇒ Object Also known as: unstub_method
Removes the stubbed versions of the given methods and restores the original methods.
75 76 77 78 79 80 |
# File 'lib/not_a_mock/object_extensions.rb', line 75 def unstub_methods(*methods) methods.each do |method, result| NotAMock::CallRecorder.instance.untrack_method(self, method) NotAMock::Stubber.instance.unstub_method(self, method) end end |
#untrack_methods(*methods) ⇒ Object Also known as: untrack_method
Stop recording calls for the given methods.
21 22 23 24 25 |
# File 'lib/not_a_mock/object_extensions.rb', line 21 def untrack_methods(*methods) methods.each do |method| NotAMock::CallRecorder.instance.untrack_method(self, method) end end |