chell
chell

Reputation: 7866

Why doesn't FactoryBot save passwords in the test database?

I have just run into a situation where I had to tack down why my test user could not login in a system test.

It turns out that the password word for the user was nil.

I ran binding.pry after a user is created:

it 'some tests do
  user = create(:user)
  binding.pry
end

   user.password = '12345' # correct
   User.last.password = nil # wtf

   user.email = '[email protected]' #correct
   User.last.email = '[email protected]' #correct

Does anyone know why passwords are not persisted into the database with FactoryBot?

Upvotes: 0

Views: 518

Answers (1)

Mark Merritt
Mark Merritt

Reputation: 2695

The reason User.last.password is nil is because the plain text password is encrypted and not accessible. Check your schema.rb file...you should only see an encrypted_password column (I'm assuming you are using Devise).

To check if the User is persisted just check user.persisted?, user.errors, or something of the sort to figure out whats going on.

Upvotes: 1

Related Questions