Class: NotAMock::Stubber

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/not_a_mock/stubber.rb

Overview

The Stubber is a singleton that keeps track of all the stub methods installed in any object.

Instance Method Summary collapse

Constructor Details

#initializeStubber

Returns a new instance of Stubber.



10
11
12
# File 'lib/not_a_mock/stubber.rb', line 10

def initialize
  @stubbed_methods = []
end

Instance Method Details

#resetObject

Removes all stub methods.



34
35
36
37
38
39
# File 'lib/not_a_mock/stubber.rb', line 34

def reset
  @stubbed_methods.each do |object, method|
    remove_hook(object, method) 
  end
  @stubbed_methods = []
end

#stub_method(object, method, &block) ⇒ Object

Stub method on object to evalutate block and return the result.

You should call Object#stub_method rathing than calling this directly.



17
18
19
20
21
22
# File 'lib/not_a_mock/stubber.rb', line 17

def stub_method(object, method, &block) #:nodoc:
  unless @stubbed_methods.include?([object, method])
    @stubbed_methods << [object, method]
    add_hook(object, method, &block)
  end
end

#unstub_method(object, method) ⇒ Object

Remove the stubbed method on object.

You should call Object#unstub_methods rather than calling this directly.



27
28
29
30
31
# File 'lib/not_a_mock/stubber.rb', line 27

def unstub_method(object, method) #:nodoc:
  if @stubbed_methods.delete([object, method])
    remove_hook(object, method)
  end
end