Class: RR::DoNotAllowCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/rr/do_not_allow_creator.rb

Overview

RR::DoNotAllowCreator uses RR::DoNotAllowCreator#method_missing to create a Scenario that expects never to be called.

The following example mocks method_name with arg1 and arg2 returning return_value.

do_not_allow(subject).method_name(arg1, arg2) { return_value }

The DoNotAllowCreator also supports a block sytnax.

do_not_allow(subject) do |m|
  m.method1 # Do not allow method1 with any arguments
  m.method2(arg1, arg2) # Do not allow method2 with arguments arg1 and arg2
  m.method3.with_no_args # Do not allow method3 with no arguments
end

Instance Method Summary collapse

Constructor Details

#initialize(space, subject) {|_self| ... } ⇒ DoNotAllowCreator

Returns a new instance of DoNotAllowCreator.

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
24
# File 'lib/rr/do_not_allow_creator.rb', line 20

def initialize(space, subject)
  @space = space
  @subject = subject
  yield(self) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &returns) ⇒ Object (protected)



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rr/do_not_allow_creator.rb', line 27

def method_missing(method_name, *args, &returns)
  double = @space.create_double(@subject, method_name)
  scenario = @space.create_scenario(double)
  if args.empty?
    scenario.with_any_args
  else
    scenario.with(*args)
  end
  scenario.never.returns(&returns)
  scenario
end