rSpec "Can't find first {objectName}"

Legacy Post!
I've flagged this post as it is either, 1) No longer relevant/useful, or 2) Doesn't reflect my current thinking. Rather than delete these outdated posts, I've left them up so that search engine links remain intact and as a history of previous perspectives.

While cleaning up several rSpec tests for a Ruby on Rails app, I ran across an error that didn’t show up in Google. It didn’t make sense that my tests kept failing.

The solution was simple, but not obvious to a new rSpec user. The clean way to specify a validation is:

it { should validate_uniqueness_of(:title) }

Unfortunately, I kept getting the error “Can’t find first Post” (where Post is the name of my object being tested. After some digging, it turns out that the shorthand notation assumes that you will have already added at least one Post to the database. It finds the first Post, then adds an identical one, expecting a failure.

This would have been fine, but my “before” block didn’t have the usual

Post.create!( :title => 'Title' )

Instead, I had:

@valid_attributes = { :title => 'Title' }

So…you can either have your before block add at least one record, or skip the clean notation and do this:

it 'should only accept unique Titles' do
  Post.create! @valid_attributes
  Post.new.should validate_uniqueness_of(:title)

That’s it!

Published September 29, 2009

other posts »