Class: NotAMock::CallRecorder

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

Overview

The CallRecorder is a singleton that keeps track of all the call recording hooks installed, and keeps a central record of calls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCallRecorder

Returns a new instance of CallRecorder.



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

def initialize
  @calls = []
  @tracked_methods = []
end

Instance Attribute Details

#callsObject (readonly)

An array of recorded calls in chronological order.

Each call is represented by a hash, in this format:

{ :object => example_object, :method => :example_method, :args => ["example argument"], :result => "example result" }


19
20
21
# File 'lib/not_a_mock/call_recorder.rb', line 19

def calls
  @calls
end

Instance Method Details

#calls_by_object(object) ⇒ Object

Return an array of all the calls made to any method of object, in chronological order.



22
23
24
# File 'lib/not_a_mock/call_recorder.rb', line 22

def calls_by_object(object)
  @calls.select {|call| call[:object] == object }
end

#calls_by_object_and_method(object, method) ⇒ Object

Return an array of all the calls made to method of object, in chronological order.



27
28
29
# File 'lib/not_a_mock/call_recorder.rb', line 27

def calls_by_object_and_method(object, method)
  @calls.select {|call| call[:object] == object && call[:method] == method }
end

#resetObject

Remove all patches so that calls are no longer recorded, and clear the call log.



60
61
62
63
# File 'lib/not_a_mock/call_recorder.rb', line 60

def reset
  untrack_all
  @calls = []
end

#track_method(object, method) ⇒ Object

Patch object so that future calls to method will be recorded.

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



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

def track_method(object, method) #:nodoc:
  unless @tracked_methods.include?([object, method])
    @tracked_methods << [object, method]
    add_hook(object, method)
  end
end

#untrack_allObject

Stop recording all calls.



52
53
54
55
56
57
# File 'lib/not_a_mock/call_recorder.rb', line 52

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

#untrack_method(object, method) ⇒ Object

Remove the patch from object so that future calls to method will not be recorded.

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



45
46
47
48
49
# File 'lib/not_a_mock/call_recorder.rb', line 45

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