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

Published by

TeddyWidom

Software Engineer

Leave a comment