S Ahmed
2011-May-16 15:29 UTC
[rspec-users] using rspec and factory_girl, how to get around attributes that can''t be set b/c of attr_accessible
class User < ActiveRecord::Base attr_accessor :password, :password_confirmation attr_accessible :password, :password_confirmation, ... validates :password, :presence => true, :confirmation => true, :length => { :within => 6..20 } end factory_girl: Factory.define :user do |u| u.user_name "test" u.password "mypassword" u.password_confirmation "mypassword" end user_spec.rb: describe User do before(:each) do @valid_att = Factory.attributes_for(:user) @user = Factory(:user) end end Now all my user spec tests pass if I comment out the line ''validates :password....'' line in my User.rb model. It seems that I can''t set the attributes password and password_confirmation in my factory_girl user factory. I use both @valid_att and @user in my tests, so I need both to contain correct values for user model. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110516/aef1306d/attachment.html>
Chris Mear
2011-May-16 16:39 UTC
[rspec-users] using rspec and factory_girl, how to get around attributes that can''t be set b/c of attr_accessible
On 16 May 2011 16:29, S Ahmed <sahmed1020 at gmail.com> wrote:> class User < ActiveRecord::Base > ??attr_accessor :password, :password_confirmation > ??attr_accessible ?:password, :password_confirmation, ... > > ??validates :password, :presence => true, :confirmation => true, :length => > { :within => 6..20 } > end > factory_girl: > Factory.define :user do |u| > ??u.user_name ?"test" > ??u.password "mypassword" > ??u.password_confirmation "mypassword" > end > user_spec.rb: > describe User do > ??before(:each) do > ?? ? ?@valid_att = Factory.attributes_for(:user) > ?? ? ?@user = Factory(:user) > ??end > > > end > > Now all my user spec tests pass if I comment out the line ''validates > :password....'' line in my User.rb model. > It seems that I can''t set the attributes password and password_confirmation > in my factory_girl user factory. > I use both @valid_att and @user in my tests, so I need both to contain > correct values for user model.It would be easier to help if you could share the error you get when your validation line is left in. Also, there is a factory_girl mailing list that might be more appropriate for this question (assuming the problem is indeed with factory_girl): https://groups.google.com/forum/#!forum/factory_girl Chris