Class: Caricature::MethodCallRecorder
- Defined in:
- lib/caricature/method_call_recorder.rb
Overview
The recorder that will collect method calls and provides an interface for finding those recordings
Instance Attribute Summary collapse
-
#method_calls ⇒ Object
readonly
gets the collection of method calls.
Instance Method Summary collapse
- #error ⇒ Object
-
#initialize ⇒ MethodCallRecorder
constructor
Initializes a new instance of a method call recorder every time a method gets called in an isolated object this gets stored in the method call recorder.
-
#record_call(method_name, mode = :instance, expectation = nil, *args, &block) ⇒ Object
records a method call or increments the count of how many times this method was called.
-
#size ⇒ Object
returns the number of different methods that has been recorderd.
-
#was_called?(method_name, block_args, mode = :instance, *args) ⇒ Boolean
returns whether the method was actually called with the specified constraints.
Constructor Details
#initialize ⇒ MethodCallRecorder
Initializes a new instance of a method call recorder every time a method gets called in an isolated object this gets stored in the method call recorder
146 147 148 |
# File 'lib/caricature/method_call_recorder.rb', line 146 def initialize @method_calls = {:instance => {}, :class => {} } end |
Instance Attribute Details
#method_calls ⇒ Object (readonly)
gets the collection of method calls. This is a hash with the method name as key
140 141 142 |
# File 'lib/caricature/method_call_recorder.rb', line 140 def method_calls @method_calls end |
Instance Method Details
#error ⇒ Object
172 173 174 |
# File 'lib/caricature/method_call_recorder.rb', line 172 def error @error end |
#record_call(method_name, mode = :instance, expectation = nil, *args, &block) ⇒ Object
records a method call or increments the count of how many times this method was called.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/caricature/method_call_recorder.rb', line 151 def record_call(method_name, mode=:instance, expectation=nil, *args, &block) mn_sym = method_name.to_s.to_sym method_calls[mode][mn_sym] ||= MethodCallRecording.new method_name mc = method_calls[mode][mn_sym] mc.count += 1 agc = mc.add_argument_variation args, block b = (expectation && expectation.block) ? (expectation.block||block) : block expectation.block = lambda do |*ags| res = nil res = b.call *ags if b agc.first.add_block_variation *ags res end if expectation block.nil? ? nil : (lambda { |*ags| res = nil res = block.call *ags if block agc.first.add_block_variation *ags res }) end |
#size ⇒ Object
returns the number of different methods that has been recorderd
195 196 197 |
# File 'lib/caricature/method_call_recorder.rb', line 195 def size @method_calls.size end |
#was_called?(method_name, block_args, mode = :instance, *args) ⇒ Boolean
returns whether the method was actually called with the specified constraints
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/caricature/method_call_recorder.rb', line 177 def was_called?(method_name, block_args, mode=:instance, *args) mc = method_calls[mode][method_name.to_s.to_sym] if mc result = mc.find_argument_variations(args, block_args).first == args @error = "Arguments don't match.\nYou expected: #{args.join(", ")}.\nI did find the following variations: #{mc.args.collect {|ar| ar.args.join(', ') }.join(' and ')}" unless result result else @error = "Couldn't find a method with name #{method_name}" return !!mc end end |