Module RR::DoubleDefinitions::DoubleDefinition::DefinitionConstructionMethods
In: lib/rr/double_definitions/double_definition.rb

Methods

Public Instance methods

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}}

[Source]

     # File lib/rr/double_definitions/double_definition.rb, line 212
212:         def after_call(&after_call_proc)
213:           raise ArgumentError, "after_call expects a block" unless after_call_proc
214:           @after_call_proc = after_call_proc
215:           self
216:         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))

[Source]

     # File lib/rr/double_definitions/double_definition.rb, line 262
262:         def implemented_by(implementation)
263:           @implementation = implementation
264:           self
265:         end

[Source]

     # File lib/rr/double_definitions/double_definition.rb, line 248
248:         def implemented_by_original_method
249:           implemented_by ORIGINAL_METHOD
250:           self
251:         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}

[Source]

     # File lib/rr/double_definitions/double_definition.rb, line 173
173:         def ordered(&return_value_block)
174:           raise(
175:             Errors::DoubleDefinitionError,
176:             "Double Definitions must have a dedicated Double to be ordered. " <<
177:             "For example, using instance_of does not allow ordered to be used. " <<
178:             "proxy the class's #new method instead."
179:           ) unless @double
180:           @ordered = true
181:           space.register_ordered_double(@double)
182:           install_method_callback return_value_block
183:           DoubleDefinitionCreateBlankSlate.new(double_definition_create)
184:         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.

[Source]

     # File lib/rr/double_definitions/double_definition.rb, line 234
234:         def returns(*args, &implementation)
235:           if !args.empty? && implementation
236:             raise ArgumentError, "returns cannot accept both an argument and a block"
237:           end
238:           if implementation
239:             install_method_callback implementation
240:           else
241:             install_method_callback(lambda do |*lambda_args|
242:               args.first
243:             end)
244:           end
245:           self
246:         end
strong()
then(&return_value_block)

Alias for ordered

Double#verbose sets the Double to print out each method call it receives.

Passing in a block sets the return value

[Source]

     # File lib/rr/double_definitions/double_definition.rb, line 221
221:         def verbose(&after_call_proc)
222:           @verbose = true
223:           @after_call_proc = after_call_proc
224:           self
225:         end

[Source]

     # File lib/rr/double_definitions/double_definition.rb, line 267
267:         def verify_method_signature
268:           @verify_method_signature = true
269:           self
270:         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|}

[Source]

     # File lib/rr/double_definitions/double_definition.rb, line 196
196:         def yields(*args, &return_value_block)
197:           @yields_value = args
198:           install_method_callback return_value_block
199:           self
200:         end

Protected Instance methods

[Source]

     # File lib/rr/double_definitions/double_definition.rb, line 274
274:         def install_method_callback(block)
275:           if block
276:             if implementation_is_original_method?
277:               after_call(&block)
278:             else
279:               implemented_by block
280:             end
281:           end
282:         end

[Validate]