Module: Quarry
- Defined in:
- lib/quarry/mockery.rb,
lib/quarry/design/spec.rb,
lib/quarry/methodprobe.rb,
lib/quarry/mock/object.rb
Defined Under Namespace
Modules: Design, Mock Classes: MethodProbe
Instance Method Summary collapse
-
#Mockery(realclass) ⇒ Object
Factory method for creating semi-functional mock object classes given the class which is to be mocked.
Instance Method Details
#Mockery(realclass) ⇒ Object
Factory method for creating semi-functional mock object classes given the class which is to be mocked. It looks like a constant for purposes of syntactic sugar.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/quarry/mockery.rb', line 25 def Mockery( realclass ) mockclass = Class.new( Mock ) mockclass.instance_eval do @mocked_class = realclass end # Provide an accessor to class instance var that holds the class # object we're faking class << mockclass # The actual class being mocked attr_reader :mocked_class # Propagate the mocked class ivar to derivatives so it can be # called like: # class MockFoo < Mock( RealClass ) def inherited( subclass ) mc = self.mockedClass subclass.instance_eval do @mocked_class = 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 = realclass.instance_methods(true).collect do |name| next if name =~ ::Mock::UnmockedMethods # Figure out the argument list arity = realclass.instance_method( name ).arity optargs = false if arity < 0 optargs = true arity = (arity+1).abs end args = [] arity.times do |n| args << "arg#{n+1}" end args << "*optargs" if optargs # Build a method definition. Some methods need special # declarations. argsj = args.join(',') case name.intern when :initialize "def initialize(#{argsj}) ; super ; end" else "def #{name}(#{argsj}) ; self.send(#{name},#{argsj}) ; end" #"def %s( %s ) ; self.__mockRegisterCall(%s) ; end" % # [ name, argstr, [":#{name}", *args].join(',') ] end end # Now add the instance methods to the mockclass class mockclass.class_eval imethods.join( "\n" ) return mockclass end |