Skip to content

Uniqueness email with factory_girl

September 22, 2011

Use sequence to create valid object with uniqueness constraint.

#Model definition
class User < ActiveRecord::Base
  validates :emal, :uniqueness => true
end

#define factory girl
Factory.sequence :email do |n|
  “user_email#{n}@domain.com”
end

Factory.define :user do |user|
  user.name "dongo"
  user.email { Factory.next(:email) }
end
Advertisements

From → Ruby, Rubyonrails

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: