Blankman
Blankman

Reputation: 267320

Factory won't set password and password_confirmation

My User.rb has:

attr_accessor :password, :password_confirmation
attr_accessible :password, :password_confirmation

My factory_girl for a user is:

Factory.define :user do |u|

  u.password "my_password"
  u.password_confirmation "my_password"

end

My User.rb sets the encrypted_password field when the object is saved.

It seems when the values that I set in my factory (the passwords), are not being set at all.

In my tests I have to do:

it "should ...." do
  user = Factory(:user)

  user.password = "abc123"

end

Why would this be the case?

I have the password attribute as both an accessor and accessible.

Is something conflicting?

Upvotes: 2

Views: 1461

Answers (1)

Paweł Gościcki
Paweł Gościcki

Reputation: 9634

The problem with the above, I believe, is that those attr_accessor fields are not persisted in the database, so they are 'lost' when declared in factories.rb. The solution for that is to set them explicitly when creating a new object from factory:

user = Factory(:user, :password => '123')

Also see:

Upvotes: 2

Related Questions