Factories failing uniqueness validations

In Everyday Rails Testing with RSpec: The Book and the related blog-post series, Aaron Sumner suggests testing FactoryGirl factories in their related model specs as such:

describe Contact do
  it "has a valid factory" do
    Factory(:contact).should be_valid
  end
end

But even with the above test in place, sometimes on more complex examples further along in the test suite, FactoryGirl has to create several contacts for me in the same RSpec example. And sometimes it fails to create any except the first one, due to uniqueness validations. So to catch this early on, I’ve started testing factories as such:

describe Contact do
  it "has a valid factory" do
    5.times do
      create(:auditor_assignment).should be_valid
    end
  end
end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s