Double#after_call creates a callback that occurs after call is called. The passed in block receives the return value of the Double being called. An Expection will be raised if no block is passed in.
mock(subject).method_name {return_value}.after_call {|return_value|}
subject.method_name # return_value
This feature is built into proxies.
mock.proxy(User).find('1') {|user| mock(user).valid? {false}}
# File lib/rr/double_definitions/double_definition.rb, line 230
230: def after_call(&after_call_proc)
231: raise ArgumentError, "after_call expects a block" unless after_call_proc
232: @after_call_proc = after_call_proc
233: self
234: end
Double#implemented_by sets the implementation of the Double. This method takes a Proc or a Method. Passing in a Method allows the Double to accept blocks.
obj = Object.new
def obj.foobar
yield(1)
end
mock(obj).method_name.implemented_by(obj.method(:foobar))
# File lib/rr/double_definitions/double_definition.rb, line 280
280: def implemented_by(implementation)
281: @implementation = implementation
282: self
283: end
# File lib/rr/double_definitions/double_definition.rb, line 266
266: def implemented_by_original_method
267: implemented_by ORIGINAL_METHOD
268: self
269: end
Double#ordered sets the Double to have an ordered expectation.
Passing in a block sets the return value.
mock(subject).method_name.ordered {return_value}
# File lib/rr/double_definitions/double_definition.rb, line 191
191: def ordered(&return_value_block)
192: raise(
193: Errors::DoubleDefinitionError,
194: "Double Definitions must have a dedicated Double to be ordered. " <<
195: "For example, using instance_of does not allow ordered to be used. " <<
196: "proxy the class's #new method instead."
197: ) unless @double
198: @ordered = true
199: space.register_ordered_double(@double)
200: install_method_callback return_value_block
201: DoubleDefinitionCreateBlankSlate.new(double_definition_create)
202: end
Double#returns accepts an argument value or a block. It will raise an ArgumentError if both are passed in.
Passing in a block causes Double to return the return value of the passed in block.
Passing in an argument causes Double to return the argument.
# File lib/rr/double_definitions/double_definition.rb, line 252
252: def returns(*args, &implementation)
253: if !args.empty? && implementation
254: raise ArgumentError, "returns cannot accept both an argument and a block"
255: end
256: if implementation
257: install_method_callback implementation
258: else
259: install_method_callback(lambda do |*lambda_args|
260: args.first
261: end)
262: end
263: self
264: end
Double#verbose sets the Double to print out each method call it receives.
Passing in a block sets the return value
# File lib/rr/double_definitions/double_definition.rb, line 239
239: def verbose(&after_call_proc)
240: @verbose = true
241: @after_call_proc = after_call_proc
242: self
243: end
# File lib/rr/double_definitions/double_definition.rb, line 285
285: def verify_method_signature
286: @verify_method_signature = true
287: self
288: end
Double#yields sets the Double to invoke a passed in block when the Double is called. An Expection will be raised if no block is passed in when the Double is called.
Passing in a block sets the return value.
mock(subject).method_name.yields(yield_arg1, yield_arg2) {return_value}
subject.method_name {|yield_arg1, yield_arg2|}
# File lib/rr/double_definitions/double_definition.rb, line 214
214: def yields(*args, &return_value_block)
215: @yields_value = args
216: install_method_callback return_value_block
217: self
218: end
# File lib/rr/double_definitions/double_definition.rb, line 292
292: def install_method_callback(block)
293: if block
294: if implementation_is_original_method?
295: after_call(&block)
296: else
297: implemented_by block
298: end
299: end
300: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.