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
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users