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.

On 16 May 2011 16:29, S Ahmed [email protected] wrote:

u.password “mypassword”
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs