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?
def random_email; alphabet = [*'a'..'z', *'A'..'Z']; alphabet.sample(15).join << "@example.com" end- sawa