Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forgery not producing unique values #43

Closed
greggroth opened this issue Oct 6, 2011 · 2 comments
Closed

forgery not producing unique values #43

greggroth opened this issue Oct 6, 2011 · 2 comments

Comments

@greggroth
Copy link

When used with FactoryGirl, i.e.

factory :user do
   email Forgery(:internet).email_address
end

and you attempt to create records:

5.times { FactoryGirl.create(:user) }

will return five records with the same e-mail address. This creates a problem if the model validates the uniqueness of the e-mail address.

@corasaurus-hex
Copy link
Contributor

Try this:

factory :user do
  email { Forgery(:internet).email_address }
end

When you just do email Forgery(:internet).email_address the email address is generated once when the factory is loaded. When you use the block form email { Forgery(:internet).email_address } the email is generated every time the factory is used to build an object. This still won't guarantee uniqueness, though, so I recommend you use sequences. They're much faster for testing as well.

@greggroth
Copy link
Author

I see. Thank you for explaining the difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants