[rspec-users] stubs which yield and return

Scott Taylor mailing_lists at railsnewbie.com
Tue Aug 7 10:51:59 EDT 2007


Is there any reason why a stub couldn't both yield and return?  I'm  
looking for a way to mock out a class I've made named "AnonymousClass":

class AnonymousClass

   def initialize(&blk)
     @klass = Class.new
     @klass.instance_eval(&blk) if block_given?
   end

   def new
     @klass.new
   end

   def evaluate(&blk)
     @klass.instance_eval(&blk)
   end

   attr_reader :klass
   alias_method :class, :klass
end

One of the behaviours of the AnonymousClass is that new can take a  
block, and eval the block in the anonymous class, represented by the  
@klass instance_variable:

ac = AnonymousClass.new do
   include Enumerable
end

So is there a way to stub AnonymousClass.new such that it yield an  
AnonymousClass mock, as well as yielding to the block given?  What  
are the technical challenges involved in implementing this in the  
mocking framework?  Or is it a matter of clean syntax?

Regards,

Scott




More information about the rspec-users mailing list