Test::Unit (Module)

In: mock.rb  (CVS)

Methods

Constants

UnmockedMethods = %r{^( __ # __id__, __call__, etc. |inspect # Useful as-is for debugging, and hard to fake |kind_of\?|is_a\?|instance_of\? # / Can't fake simply -- delegated to the |type|class # \ actual underlying class |method|send|respond_to\? # These will work fine as-is |hash # There's no good way to fake this )}x
  The Regexp that matches methods which will not be mocked.

Classes and Modules

Class Test::Unit::Mockup

Public Class methods

Factory method for creating semi-functional mock objects given the class which is to be mocked. It looks like a constant for purposes of syntactic sugar.

[Source]

# File mock.rb, line 416
        def self::MockObject( klass )
            mockup = Class::new( Mockup )
            mockup.instance_eval do @mockedClass = klass end

            ### Provide an accessor to class instance var that holds the class
            ### object we're faking
            class << mockup

                # The actual class being mocked
                attr_reader :mockedClass

                ### Propagate the mocked class ivar to derivatives so it can be
                ### called like:
                ###   class MockFoo < Test::Unit::MockObject( RealClass )
                def inherited( subclass )
                    mc = self.mockedClass
                    subclass.instance_eval do @mockedClass = mc end
                end
            end
            
            # Build method definitions for all the mocked class's instance
            # methods, as well as those given to it by its superclasses, since
            # we're not really inheriting from it.
            imethods = klass.instance_methods(true).collect {|name|
                next if name =~ UnmockedMethods

                # Figure out the argument list
                argCount = klass.instance_method( name ).arity
                optionalArgs = false

                if argCount < 0
                    optionalArgs = true
                    argCount = (argCount+1).abs
                end
                
                args = []
                argCount.times do |n| args << "arg#{n+1}" end
                args << "*optionalArgs" if optionalArgs

                # Build a method definition. Some methods need special
                # declarations.
                case name.intern
                when :initialize
                    "def initialize( %s ) ; super ; end" % args.join(',')

                else
                    "def %s( %s ) ; self.__mockRegisterCall(%s) ; end" %
                        [ name, args.join(','), [":#{name}", *args].join(',') ]
                end
            }

            # Now add the instance methods to the mockup class
            mockup.class_eval imethods.join( "\n" )
            return mockup
        end

[Validate]