[rspec-users] have_one and have_present

Luis Lavena luislavena at gmail.com
Fri May 4 23:34:41 EDT 2007

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

> 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

Piston the gem just for testing is also questionable, but doable.

> Cheers,
> David

Again, thank for your your feedback,


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

More information about the rspec-users mailing list