[rspec-users] have_one and have_present

David Chelimsky dchelimsky at gmail.com
Fri May 4 23:38:35 EDT 2007


On 5/4/07, Luis Lavena <luislavena at gmail.com> wrote:
> On 5/5/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > >Comment By: Luis Lavena (luislavena)
> > > Date: 2007-05-04 23:37
> >
> > > describe "An Asset" do
> > >   before(:each) do
> > >     @asset = Asset.new
> > >   end
> > >
> > >   it { @asset.should have_one(:attachment) }
> > >   it { @asset.should have_present(:something) }
> > > end
> >
> > Food for thought on these. I like have_one a lot. It speaks to me as a
> > Rails developer and I think it speaks to a customer as well.
> >
> > On the flip side, 'have_present' doesn't really tell me what is
> > interesting about having it present, or why it should be present. I
> > think 'require_attribute' would be nice ('require' would be even
> > better, but obviously that is a keyword). Or 'validate_presence_of':
> >
> > an { @asset.should validate_presence_of(:something) }
> >
> > That's a little more Rails-developer-friendly than customer-friendly,
> > but it still 'speaks' better than 'have_present' for me.
> >
>
> yeah, have_present was due "require" been reserved word :-P
>
> validate_presence_of was another option, but that will force me to use
> validate_uniqueness_of, etc...
>
> A lot of typing just for one liner of example :-P

FWIW, you only have to type these things once each time you type them.
You're going to read them hundreds of times. It's worth the extra
typing in my view! If the examples are documentation, they should tell
you, with as little mental mapping as possible, what it is that's
being described. Using 'validate_presence_of' tells me EXACTLY what
the code should look like. That's worth a lot to me.

Cheers,
David

>
> > FYI - Jay Fields has a Validatable framework that includes some
> > test/unit assertions that look like this:
> >
> > Foo.must_validate do
> >   presence_of :name
> >   format_of(:name).with(/^[A-Z]/)
> >   numericality_of(:age).only_integer(true)
> > end
> >
> > Because Spec::Rails behaviours inherit from Test::Unit::TestCase, you
> > can install the validations gem (gem install validatable) and use
> > these as/is with RSpec. Or, if  you want it to feel more spec'ish, you
> > could monkey patch an alias:
> >
>
> I wasn't aware of it, thanks for the tip.
>
> > Foo.should_validate do
> >   presence_of :name
> >   format_of(:name).with(/^[A-Z]/)
> >   numericality_of(:age).only_integer(true)
> > end
> >
> > Whether you choose to use his framework or write your own, I think
> > there is something to be learned from its expressiveness.
> >
>
> Of course, I often try to keep small the set of plugins/gems I use
> during development, which require I do gem install there to get the
> functionality.
>
> Piston the gem just for testing is also questionable, but doable.
>
> > Cheers,
> > David
>
> Again, thank for your your feedback,
>
> Regards,
>
> --
> Luis Lavena
> Multimedia systems
> -
> Leaders are made, they are not born. They are made by hard effort,
> which is the price which all of us must pay to achieve any goal that
> is worthwhile.
> Vince Lombardi
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list