Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting protected attributes with FactoryGirl

FactoryGirl won't set my protected attribute user.confirmed. What's the best practice here?

Factory.define :user do |f|
  f.name "Tim"          # attr_accessible -- this works
  f.confirmed true      # attr_protected -- doesn't work
end 

I can do a @user.confirmed = true after using my factory, but that's a lot of repetition across a lot of tests.

like image 273
brittohalloran Avatar asked Jan 18 '12 05:01

brittohalloran


2 Answers

Using an after_create hook works:

Factory.define :user do |f|
  f.name "Tim"
  f.after_create do |user|
    user.confirmed = true
    user.save
  end
end 
like image 122
brittohalloran Avatar answered Sep 21 '22 04:09

brittohalloran


You would have to pass it into the hash when you create the user since FactoryGirl is protecting it from mass-assignment.

user ||= Factory(:user, :confirmed => true)
like image 36
iwasrobbed Avatar answered Sep 19 '22 04:09

iwasrobbed