08 June 2012

Intermittantly failing specs are infuriating.

So, a word of warning: If you’re using FFaker or something to generate values in your object generation library (in our case, that would be Fabrication), be careful with uniqueness validations.

Yes, it’s obvious how to fix now. But it’s not so obvious when you add a uniqueness validation, forget to change the object generator, run the tests, find that they happen to pass (that time), and then later you’re wrestling with intermittantly failing specs. And because it’s no longer fresh in your mind, you have no idea why.

Here’s the obvious fix, implemented in Fabrication (with FFaker, still):

Fabricator :account do
  # this column has a uniqueness validation
  name { sequence(:account_name) { |i| "#{Faker::Company.name} {i}" } }
end

That’s a bit paranoid, but consider that originally we had

name Faker::Lorem.word

instead, which was causing lots of random collisions.

Well, I hope that helps someone.



blog comments powered by Disqus