DO NOT USE FREE ATTRIBUTES
One common pattern is to use a fake data library (like Faker or Forgery) to generate random values ββper fly. This may seem appealing to names, email addresses, or phone numbers, but it does not have a real purpose. Creating unique values ββis simple enough with sequences:
FactoryGirl.define do sequence(:title) { |n| "Example title #{n}" } factory :post do title end end FactoryGirl.create(:post).title
Your randomized data may at some point trigger unexpected results in your tests, which makes your factories disappointing to work with. Any value that may affect the test result in some way will have to be redefined, which means:
Over time, you will discover new attributes that sometimes cause. This disappoints the process, because tests can only once every ten or hundreds of runs - depending on how many attributes and possible values ββare, and which combination causes the error. You will need to list each such random attribute in each test in order to override it, which is stupid. Thus, you create non-random factories, thereby denying any benefit from the original chance. It can be argued, as Henrik Sniff does, that random values ββhelp you detect errors. While this is possible, it means you have a bigger problem: holes in your test suite. In the worst case, the error still goes unnoticed; at best, you get a cryptic error message that disappears the next time you run the test, it's hard to debug. True, a critical error is better than an error, but randomized factories remain a poor substitute for proper unit testing, code, and TDD to prevent these problems.
Therefore, randomized factories are not only not worth the effort, they even give you false confidence in their tests, which is worse than not having any tests at all.
But you have nothing to do, if you want, just do it.
Oh, and thereβs an even easier way to embed a sequence in a recent FactoryGirl, this quote was written for an older version.