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.



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

def initialize
  @stubbed_methods = {}
end

Instance Method Details

#get_stubmethod(object, method) ⇒ Object

Retrieve the stub method data and code for stubbed method on the object



47
48
49
# File 'lib/not_a_mock/stubber.rb', line 47

def get_stubmethod(object, method)
	@stubbed_methods[[object, method]]
end

#resetObject

Removes all stub methods.



37
38
39
40
41
42
43
44
# File 'lib/not_a_mock/stubber.rb', line 37

def reset
  @stubbed_methods.each do |key, value|
  	object = key[0]
  	method = key[1]
    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.



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

def stub_method(object, method, &block) #:nodoc:
  unless @stubbed_methods.include?([object, method])
  	stubmethod = NotAMock::StubMethod.new(&block)
    @stubbed_methods[[object, method]] = stubmethod
    add_hook(object, method)
  end
  stubmethod
end

#unstub_method(object, method) ⇒ Object

Remove the stubbed method on object.

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



30
31
32
33
34
# File 'lib/not_a_mock/stubber.rb', line 30

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