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
On 8/7/07, Scott Taylor <mailing_lists at railsnewbie.com> wrote:> > Is there any reason why a stub couldn''t both yield and return?Yes. Nobody ever asked for it :)> 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?Again - this simply hasn''t come up. Please add an RFE and feel free to submit a patch. Cheers, David> > Regards, > > Scott > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On Aug 7, 2007, at 12:54 PM, David Chelimsky wrote:> On 8/7/07, Scott Taylor <mailing_lists at railsnewbie.com> wrote: >> >> Is there any reason why a stub couldn''t both yield and return? > > Yes. Nobody ever asked for it :) > > Again - this simply hasn''t come up. Please add an RFE and feel free to > submit a patch. >Will do. I think for now I''m going to override the stub in the individual spec to yield. More importantly, I''m going to get autotest to work w/ rspec''s native development before I do anymore rspec work. The time to run the full test suite is absolutely unbearable on my Mac G4. Scott