Hello all, I trust you are having a good one. I''m just trying out macros and my first one is taken from the RSpec book which replaces ..... @obj.should_not be_valid @obj.should have(1).error_on(:attr) with the macro.... should_require_attribute Model, :attribute My first attempt is basic but already I have problems. The following is called as it_should_require_attribute Model, :attribute but I would rather call if as it_should_require_attribute @model, :attribute. I prefer this call as I want to use what has been setup in my before(:each) block -- setting @model and all other housekeeping stuff required for the tests -- and I can then remove dut_instance = dut.new from the macro. Alas, my before(:each) block is not being called!! I thought it would be called when the it in the macro is met. module ModelMacros ## dut - Domain Under Test ## aut - Attribute Under Test def it_should_require_attribute(dut, aut) it "is not valid with missing #{aut}" do dut_instance = dut.new dut_instance.send("#{aut}=", nil) dut_instance.should_not be_valid dut_instance.should have(1).error_on(aut.to_sym) end end end Could anyone give me some tips on how to approach macros better for seamless integration into my tests. Thank you -ants -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20111226/5e465eee/attachment.html>
On Dec 26, 2011, at 10:06 AM, Ants Pants wrote:> Hello all, > > I trust you are having a good one. > > I''m just trying out macros and my first one is taken from the RSpec book which replaces ..... > > @obj.should_not be_valid > @obj.should have(1).error_on(:attr) > > with the macro.... > > should_require_attribute Model, :attribute > > My first attempt is basic but already I have problems. > > The following is called as > > it_should_require_attribute Model, :attribute > > but I would rather call if as > > it_should_require_attribute @model, :attribute. > > I prefer this call as I want to use what has been setup in my before(:each) block -- setting @model and all other housekeeping stuff required for the tests -- and I can then remove dut_instance = dut.new from the macro. > > Alas, my before(:each) block is not being called!! I thought it would be called when the it in the macro is met. > > module ModelMacros > ## dut - Domain Under Test > ## aut - Attribute Under Test > > def it_should_require_attribute(dut, aut) > it "is not valid with missing #{aut}" do > dut_instance = dut.new > dut_instance.send("#{aut}=", nil) > dut_instance.should_not be_valid > dut_instance.should have(1).error_on(aut.to_sym) > end > end > end > > Could anyone give me some tips on how to approach macros better for seamless integration into my tests. >I like to use self.described_class instead of passing classes or instances in. Here is an example: def it_should_require_attribute(attribute) klass = self.described_class instance = klass.new instance.send("#{attribute}=", nil) instance.should_not be_valid instance.should have(1).error_on(attribute.to_sym) end describe MyModel do it_should_require_attribute :some_attribute end I''m still using RSpec 1.x on Rails 2.3.x projects and have no idea if this is supported in RSpec 2.x. Peace.
On 26 December 2011 18:01, Phillip Koebbe <phillip.koebbe at gmail.com> wrote:> > On Dec 26, 2011, at 10:06 AM, Ants Pants wrote: > > > Hello all, > > > > I trust you are having a good one. > > > > I''m just trying out macros and my first one is taken from the RSpec book > which replaces ..... > > > > @obj.should_not be_valid > > @obj.should have(1).error_on(:attr) > > > > with the macro.... > > > > should_require_attribute Model, :attribute > > > > My first attempt is basic but already I have problems. > > > > The following is called as > > > > it_should_require_attribute Model, :attribute > > > > but I would rather call if as > > > > it_should_require_attribute @model, :attribute. > > > > I prefer this call as I want to use what has been setup in my > before(:each) block -- setting @model and all other housekeeping stuff > required for the tests -- and I can then remove dut_instance = dut.new from > the macro. > > > > Alas, my before(:each) block is not being called!! I thought it would be > called when the it in the macro is met. > > > > module ModelMacros > > ## dut - Domain Under Test > > ## aut - Attribute Under Test > > > > def it_should_require_attribute(dut, aut) > > it "is not valid with missing #{aut}" do > > dut_instance = dut.new > > dut_instance.send("#{aut}=", nil) > > dut_instance.should_not be_valid > > dut_instance.should have(1).error_on(aut.to_sym) > > end > > end > > end > > > > Could anyone give me some tips on how to approach macros better for > seamless integration into my tests. > > > > I like to use self.described_class instead of passing classes or instances > in. Here is an example: > > def it_should_require_attribute(attribute) > klass = self.described_class > instance = klass.new > instance.send("#{attribute}=", nil) > instance.should_not be_valid > instance.should have(1).error_on(attribute.to_sym) > end > > describe MyModel do > it_should_require_attribute :some_attribute > end > > I''m still using RSpec 1.x on Rails 2.3.x projects and have no idea if this > is supported in RSpec 2.x. > > Peace. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >Thanks for your reply. Firstly, I was mistaken, my before block is being called when the it block is met in my macro. I''m using rspec-rails 1.3.4 and Rails 2.3.11 Your code doesn''t really help, though. It''s just another way of addressing what I''m already doing. As I now know my before block is begin called and I have access to an instance variable, I tried a generic name @dut (Domain Under Test) and that works. But, I have now lost a meaningful instance variable name for non macro tests. Maybe I could just #dup them. Is there a better way? I reckon there is and will find it while playing. -ants -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20111226/f1547398/attachment-0001.html>
On Dec 26, 2011, at 12:09 PM, Ants Pants wrote:> > > On 26 December 2011 18:01, Phillip Koebbe <phillip.koebbe at gmail.com> wrote: > > On Dec 26, 2011, at 10:06 AM, Ants Pants wrote: > > > Hello all, > > > > I trust you are having a good one. > > > > I''m just trying out macros and my first one is taken from the RSpec book which replaces ..... > > > > @obj.should_not be_valid > > @obj.should have(1).error_on(:attr) > > > > with the macro.... > > > > should_require_attribute Model, :attribute > > > > My first attempt is basic but already I have problems. > > > > The following is called as > > > > it_should_require_attribute Model, :attribute > > > > but I would rather call if as > > > > it_should_require_attribute @model, :attribute. > > > > I prefer this call as I want to use what has been setup in my before(:each) block -- setting @model and all other housekeeping stuff required for the tests -- and I can then remove dut_instance = dut.new from the macro. > > > > Alas, my before(:each) block is not being called!! I thought it would be called when the it in the macro is met. > > > > module ModelMacros > > ## dut - Domain Under Test > > ## aut - Attribute Under Test > > > > def it_should_require_attribute(dut, aut) > > it "is not valid with missing #{aut}" do > > dut_instance = dut.new > > dut_instance.send("#{aut}=", nil) > > dut_instance.should_not be_valid > > dut_instance.should have(1).error_on(aut.to_sym) > > end > > end > > end > > > > Could anyone give me some tips on how to approach macros better for seamless integration into my tests. > > > > I like to use self.described_class instead of passing classes or instances in. Here is an example: > > def it_should_require_attribute(attribute) > klass = self.described_class > instance = klass.new > instance.send("#{attribute}=", nil) > instance.should_not be_valid > instance.should have(1).error_on(attribute.to_sym) > end > > describe MyModel do > it_should_require_attribute :some_attribute > end > > I''m still using RSpec 1.x on Rails 2.3.x projects and have no idea if this is supported in RSpec 2.x. > > Peace. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > Thanks for your reply. > > Firstly, I was mistaken, my before block is being called when the it block is met in my macro. > > I''m using rspec-rails 1.3.4 and Rails 2.3.11 > > Your code doesn''t really help, though. It''s just another way of addressing what I''m already doing. As I now know my before block is begin called and I have access to an instance variable, I tried a generic name @dut (Domain Under Test) and that works. But, I have now lost a meaningful instance variable name for non macro tests. Maybe I could just #dup them. >Sorry, I should have been more specific. I wasn''t addressing the issue you were having with the before block. I was addressing this:> Could anyone give me some tips on how to approach macros better for seamless integration into my tests.Which I understood to mean that if anyone had any ideas about how to make the tests simpler/dryer/etc, you''d be happy to hear them. Sorry if I misunderstood and you didn''t find it useful. Peace. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20111226/386b080a/attachment.html>
On 26 December 2011 22:09, Phillip Koebbe <phillip.koebbe at gmail.com> wrote:> > On Dec 26, 2011, at 12:09 PM, Ants Pants wrote: > > > > On 26 December 2011 18:01, Phillip Koebbe <phillip.koebbe at gmail.com>wrote: > >> >> On Dec 26, 2011, at 10:06 AM, Ants Pants wrote: >> >> > Hello all, >> > >> > I trust you are having a good one. >> > >> > I''m just trying out macros and my first one is taken from the RSpec >> book which replaces ..... >> > >> > @obj.should_not be_valid >> > @obj.should have(1).error_on(:attr) >> > >> > with the macro.... >> > >> > should_require_attribute Model, :attribute >> > >> > My first attempt is basic but already I have problems. >> > >> > The following is called as >> > >> > it_should_require_attribute Model, :attribute >> > >> > but I would rather call if as >> > >> > it_should_require_attribute @model, :attribute. >> > >> > I prefer this call as I want to use what has been setup in my >> before(:each) block -- setting @model and all other housekeeping stuff >> required for the tests -- and I can then remove dut_instance = dut.new from >> the macro. >> > >> > Alas, my before(:each) block is not being called!! I thought it would >> be called when the it in the macro is met. >> > >> > module ModelMacros >> > ## dut - Domain Under Test >> > ## aut - Attribute Under Test >> > >> > def it_should_require_attribute(dut, aut) >> > it "is not valid with missing #{aut}" do >> > dut_instance = dut.new >> > dut_instance.send("#{aut}=", nil) >> > dut_instance.should_not be_valid >> > dut_instance.should have(1).error_on(aut.to_sym) >> > end >> > end >> > end >> > >> > Could anyone give me some tips on how to approach macros better for >> seamless integration into my tests. >> > >> >> I like to use self.described_class instead of passing classes or >> instances in. Here is an example: >> >> def it_should_require_attribute(attribute) >> klass = self.described_class >> instance = klass.new >> instance.send("#{attribute}=", nil) >> instance.should_not be_valid >> instance.should have(1).error_on(attribute.to_sym) >> end >> >> describe MyModel do >> it_should_require_attribute :some_attribute >> end >> >> I''m still using RSpec 1.x on Rails 2.3.x projects and have no idea if >> this is supported in RSpec 2.x. >> >> Peace. >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > Thanks for your reply. > > Firstly, I was mistaken, my before block is being called when the it block > is met in my macro. > > I''m using rspec-rails 1.3.4 and Rails 2.3.11 > > Your code doesn''t really help, though. It''s just another way of addressing > what I''m already doing. As I now know my before block is begin called and I > have access to an instance variable, I tried a generic name @dut (Domain > Under Test) and that works. But, I have now lost a meaningful instance > variable name for non macro tests. Maybe I could just #dup them. > > > Sorry, I should have been more specific. I wasn''t addressing the issue you > were having with the before block. I was addressing this: > > Could anyone give me some tips on how to approach macros better for > seamless integration into my tests. > > > Which I understood to mean that if anyone had any ideas about how to make > the tests simpler/dryer/etc, you''d be happy to hear them. Sorry if I > misunderstood and you didn''t find it useful. > > Peace. > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >I always find replies useful and I thanked you for it. I didn''t know about described_class, for example, so I learned something there. My request for having my macros more seamless was to do with them using the current environment they were running in (before blocks etc.) I have solved that by not being so dupid. I think too much good food over Christmas has made me even softer. -ants -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20111227/dd07c8b1/attachment.html>