Reputation: 5782
I'm using RSpec to test my site. I have a method that generates random email addresses for users to sign up with:
def random_email
alphabet = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten
(0...15).map{ alphabet[rand(alphabet.length)] }.join << "@example.com"
end
This method is called multiple times throughout the test suite. Many times, it ends up generating either A) multiple identical email addresses (actual example: [email protected]
), or B) email addresses that add a few new characters to the beginning of a previously used email address, and chop a few off the end (actual example: [email protected]
and [email protected]
).
I found that pseudo random generators work in a sequence, such that when the randomness is re-seeded with the same number, it will do the same sequence over again. I can't help wondering if RSpec is re-seeding Ruby's randomness between tests. (Perhaps it's worth noting that the tests that reuse email addresses often occur in different RSpec test files; maybe RSpec is re-seeding in between files?)
Any ideas?
Upvotes: 1
Views: 179
Reputation: 23939
Even if RSpec isn't re-seeding the RNG, something else may, and it'll break your specs. You're making your specs fragile by tying their behavior to some greater system state.
Often unique email address are just generated sequentially, either using something like FactoryGirl or just in a helper method. If you run out of those, congratulate yourself on writing the largest test suite in history.
Upvotes: 3