Class: Object

Inherits:
BasicObject
Defined in:
lib/not_a_mock/object_extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

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.



87
88
89
# File 'lib/not_a_mock/object_extensions.rb', line 87

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.



101
102
103
# File 'lib/not_a_mock/object_extensions.rb', line 101

def meta_eval(&block)
  metaclass.instance_eval(&block)
end

#metaclassObject

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



94
95
96
97
98
# File 'lib/not_a_mock/object_extensions.rb', line 94

def metaclass
  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
# File 'lib/not_a_mock/object_extensions.rb', line 36

def stub_method(method, &block)
  case method
    when Symbol
      NotAMock::CallRecorder.instance.untrack_method(self, method)
      NotAMock::Stubber.instance.unstub_method(self, method)
      NotAMock::Stubber.instance.stub_method(self, method, &block)
      NotAMock::CallRecorder.instance.track_method(self, method)
    when Hash
      stub_methods(method)
    else
      raise ArgumentError
  end
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.



56
57
58
59
60
# File 'lib/not_a_mock/object_extensions.rb', line 56

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.



64
65
66
67
68
# File 'lib/not_a_mock/object_extensions.rb', line 64

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.



73
74
75
76
77
78
# File 'lib/not_a_mock/object_extensions.rb', line 73

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