On 26 December 2011 18:01, Phillip Koebbe <phillip.koe...@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@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
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to