Module | RR::DoubleDefinitions::DoubleDefinition::DefinitionConstructionMethods |
In: |
lib/rr/double_definitions/double_definition.rb
|
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 214 214: def after_call(&after_call_proc) 215: raise ArgumentError, "after_call expects a block" unless after_call_proc 216: @after_call_proc = after_call_proc 217: self 218: 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 264 264: def implemented_by(implementation) 265: @implementation = implementation 266: self 267: end
# File lib/rr/double_definitions/double_definition.rb, line 250 250: def implemented_by_original_method 251: implemented_by ORIGINAL_METHOD 252: self 253: 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 175 175: def ordered(&return_value_block) 176: raise( 177: Errors::DoubleDefinitionError, 178: "Double Definitions must have a dedicated Double to be ordered. " << 179: "For example, using instance_of does not allow ordered to be used. " << 180: "proxy the class's #new method instead." 181: ) unless @double 182: @ordered = true 183: space.register_ordered_double(@double) 184: install_method_callback return_value_block 185: DoubleDefinitionCreateBlankSlate.new(double_definition_create) 186: 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 236 236: def returns(*args, &implementation) 237: if !args.empty? && implementation 238: raise ArgumentError, "returns cannot accept both an argument and a block" 239: end 240: if implementation 241: install_method_callback implementation 242: else 243: install_method_callback(lambda do |*lambda_args| 244: args.first 245: end) 246: end 247: self 248: 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 223 223: def verbose(&after_call_proc) 224: @verbose = true 225: @after_call_proc = after_call_proc 226: self 227: end
# File lib/rr/double_definitions/double_definition.rb, line 269 269: def verify_method_signature 270: @verify_method_signature = true 271: self 272: 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 198 198: def yields(*args, &return_value_block) 199: @yields_value = args 200: install_method_callback return_value_block 201: self 202: end