[rspec-devel] have_one and have_present

David Chelimsky dchelimsky at gmail.com
Fri May 4 23:25:12 EDT 2007

>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.

FYI - Jay Fields has a Validatable framework that includes some
test/unit assertions that look like this:

Foo.must_validate do
  presence_of :name

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:

Foo.should_validate do
  presence_of :name

Whether you choose to use his framework or write your own, I think
there is something to be learned from its expressiveness.


More information about the rspec-devel mailing list