| Class | Object |
| In: |
lib/not_a_mock/object_extensions.rb
|
| Parent: | 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.
# File lib/not_a_mock/object_extensions.rb, line 83
83: def stub_instance(methods = {})
84: NotAMock::Stub.new(self, methods)
85: end
Returns the metaclass of this object. For an explanation of metaclasses, see: whytheluckystiff.net/articles/seeingMetaclassesClearly.html
# File lib/not_a_mock/object_extensions.rb, line 90
90: def metaclass
91: class << self
92: self
93: end
94: end
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.
# File lib/not_a_mock/object_extensions.rb, line 32
32: def stub_method(method, &block)
33: case method
34: when Symbol
35: NotAMock::CallRecorder.instance.untrack_method(self, method)
36: NotAMock::Stubber.instance.unstub_method(self, method)
37: NotAMock::Stubber.instance.stub_method(self, method, &block)
38: NotAMock::CallRecorder.instance.track_method(self, method)
39: when Hash
40: stub_methods(method)
41: else
42: raise ArgumentError
43: end
44: end
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.
# File lib/not_a_mock/object_extensions.rb, line 52
52: def stub_methods(methods)
53: methods.each do |method, result|
54: stub_method(method) {|*args| result }
55: end
56: end
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.
# File lib/not_a_mock/object_extensions.rb, line 60
60: def stub_methods_to_raise(methods)
61: methods.each do |method, exception|
62: stub_method(method) {|*args| raise exception }
63: end
64: end
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.
# File lib/not_a_mock/object_extensions.rb, line 8
8: def track_methods(*methods)
9: methods.each do |method|
10: NotAMock::CallRecorder.instance.track_method(self, method)
11: end
12: end
Removes the stubbed versions of the given methods and restores the original methods.
# File lib/not_a_mock/object_extensions.rb, line 69
69: def unstub_methods(*methods)
70: methods.each do |method, result|
71: NotAMock::CallRecorder.instance.untrack_method(self, method)
72: NotAMock::Stubber.instance.unstub_method(self, method)
73: end
74: end