Sha256: 657b9fad6388ad7d097d5ff05fed60bd214ee7bd1cdada804048957c0ab93ab6

Contents?: true

Size: 1.17 KB

Versions: 12

Compression:

Stored size: 1.17 KB

Contents

module RR
  # 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
  class DoNotAllowCreator
    instance_methods.each { |m| undef_method m unless m =~ /^__/ }
    
    def initialize(space, subject)
      @space = space
      @subject = subject
      yield(self) if block_given?
    end

    protected
    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
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rr-0.1.10 lib/rr/do_not_allow_creator.rb
rr-0.1.4 lib/rr/do_not_allow_creator.rb
rr-0.1.7 lib/rr/do_not_allow_creator.rb
rr-0.1.1 lib/rr/do_not_allow_creator.rb
rr-0.1.3 lib/rr/do_not_allow_creator.rb
rr-0.1.11 lib/rr/do_not_allow_creator.rb
rr-0.1.8 lib/rr/do_not_allow_creator.rb
rr-0.1.0 lib/rr/do_not_allow_creator.rb
rr-0.1.6 lib/rr/do_not_allow_creator.rb
rr-0.1.5 lib/rr/do_not_allow_creator.rb
rr-0.1.2 lib/rr/do_not_allow_creator.rb
rr-0.1.9 lib/rr/do_not_allow_creator.rb