Skip to content

Instantly share code, notes, and snippets.

@cnk
Created January 10, 2012 02:59
Show Gist options
  • Save cnk/1586603 to your computer and use it in GitHub Desktop.
Save cnk/1586603 to your computer and use it in GitHub Desktop.
FactoryGirl and protected attributes
class User < ActiveRecord::Base
# devise configuration info omitted
# admin column is defined with :default => false
attr_accessible :email, :password, :password_confirmation
end
FactoryGirl.define do
trait :admin do
sequence(:email) { |n| "admin#{n}@ticketee.com" }
admin true
end
factory :user do
sequence(:email) { |n| "user#{n}@ticketee.com" }
password "password"
password_confirmation "password"
confirmed_at { Time.now() }
factory :admin, :traits => [:admin]
end
end
# admin factory does not work!
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "user#{n}@ticketee.com" }
password "password"
password_confirmation "password"
confirmed_at { Time.now() }
end
factory :admin, :class => User do
sequence(:email) { |n| "admin#{n}@ticketee.com" }
password "password"
password_confirmation "password"
confirmed_at { Time.now() }
admin true
end
end
@one = FactoryGirl.create(:admin)
# @one is created but @one.admin => false
@two = FactoryGirl.create(:user, :admin => true)
# @two is created and @two.admin => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment