Class Object
In: lib/not_a_mock/object_extensions.rb
Parent: Object

Methods

Public Class methods

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.

[Source]

    # File lib/not_a_mock/object_extensions.rb, line 83
83:     def stub_instance(methods = {})
84:       NotAMock::Stub.new(self, methods)
85:     end

Public Instance methods

log_calls_to(*methods)

Alias for track_methods

Evaluates the block in the context of this object‘s metaclass.

[Source]

    # File lib/not_a_mock/object_extensions.rb, line 97
97:   def meta_eval(&block)
98:     metaclass.instance_eval(&block)
99:   end

Returns the metaclass of this object. For an explanation of metaclasses, see: whytheluckystiff.net/articles/seeingMetaclassesClearly.html

[Source]

    # 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.

[Source]

    # 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
stub_method_to_raise(methods)

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.

[Source]

    # 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.

[Source]

    # 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
track_method(*methods)

Alias for track_methods

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.

[Source]

    # 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
unstub_method(*methods)

Alias for unstub_methods

Removes the stubbed versions of the given methods and restores the original methods.

[Source]

    # 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
untrack_method(*methods)

Alias for untrack_methods

Stop recording calls for the given methods.

[Source]

    # File lib/not_a_mock/object_extensions.rb, line 17
17:   def untrack_methods(*methods)
18:     methods.each do |method|
19:       NotAMock::CallRecorder.instance.untrack_method(self, method)
20:     end
21:   end

[Validate]