Module: RR::DoubleDefinitions::DoubleDefinition::DefinitionConstructionMethods
- Included in:
- RR::DoubleDefinitions::DoubleDefinition
- Defined in:
- lib/rr/double_definitions/double_definition.rb
Instance Method Summary collapse
-
#after_call(&after_call_proc) ⇒ Object
Double#after_call creates a callback that occurs after call is called.
-
#implemented_by(implementation) ⇒ Object
Double#implemented_by sets the implementation of the Double.
- #implemented_by_original_method ⇒ Object
-
#ordered(&return_value_block) ⇒ Object
(also: #then)
Double#ordered sets the Double to have an ordered expectation.
-
#returns(*args, &implementation) ⇒ Object
Double#returns accepts an argument value or a block.
-
#verbose(&after_call_proc) ⇒ Object
Double#verbose sets the Double to print out each method call it receives.
- #verify_method_signature ⇒ Object (also: #strong)
-
#yields(*args, &return_value_block) ⇒ Object
Double#yields sets the Double to invoke a passed in block when the Double is called.
Instance Method Details
#after_call(&after_call_proc) ⇒ Object
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}}
214 215 216 217 218 |
# File 'lib/rr/double_definitions/double_definition.rb', line 214 def after_call(&after_call_proc) raise ArgumentError, "after_call expects a block" unless after_call_proc @after_call_proc = after_call_proc self end |
#implemented_by(implementation) ⇒ Object
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.
yield(1)
end
mock(obj).method_name.implemented_by(obj.method(:foobar))
264 265 266 267 |
# File 'lib/rr/double_definitions/double_definition.rb', line 264 def implemented_by(implementation) @implementation = implementation self end |
#implemented_by_original_method ⇒ Object
250 251 252 253 |
# File 'lib/rr/double_definitions/double_definition.rb', line 250 def implemented_by_original_method implemented_by ORIGINAL_METHOD self end |
#ordered(&return_value_block) ⇒ Object Also known as: then
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}
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/rr/double_definitions/double_definition.rb', line 175 def ordered(&return_value_block) raise( Errors::DoubleDefinitionError, "Double Definitions must have a dedicated Double to be ordered. " << "For example, using instance_of does not allow ordered to be used. " << "proxy the class's #new method instead." ) unless @double @ordered = true space.register_ordered_double(@double) install_method_callback return_value_block DoubleDefinitionCreateBlankSlate.new(double_definition_create) end |
#returns(*args, &implementation) ⇒ Object
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.
236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/rr/double_definitions/double_definition.rb', line 236 def returns(*args, &implementation) if !args.empty? && implementation raise ArgumentError, "returns cannot accept both an argument and a block" end if implementation install_method_callback implementation else install_method_callback(lambda do |*lambda_args| args.first end) end self end |
#verbose(&after_call_proc) ⇒ Object
Double#verbose sets the Double to print out each method call it receives.
Passing in a block sets the return value
223 224 225 226 227 |
# File 'lib/rr/double_definitions/double_definition.rb', line 223 def verbose(&after_call_proc) @verbose = true @after_call_proc = after_call_proc self end |
#verify_method_signature ⇒ Object Also known as: strong
269 270 271 272 |
# File 'lib/rr/double_definitions/double_definition.rb', line 269 def verify_method_signature @verify_method_signature = true self end |
#yields(*args, &return_value_block) ⇒ Object
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|}
198 199 200 201 202 |
# File 'lib/rr/double_definitions/double_definition.rb', line 198 def yields(*args, &return_value_block) @yields_value = args install_method_callback return_value_block self end |