should_validate_uniqueness_of snippets

How to fix Shoulda's “Can't find first xxx” problem

Tagged shoulda, gotcha, should_validate_uniqueness_of  Languages ruby

In the following example, Shoulda's should_validate_uniqueness_of might throw an “Can't find first” error:

class PostTest < ActiveSupport::TestCase
  should_validate_uniqueness_of :title

To fix it add a subject to the test:

class PostTest < ActiveSupport::TestCase
  subject { Factory(:post) }
  should_validate_uniqueness_of :title