Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4619

How to prevent password from being updated if empty in Rails Model and test it with RSpec?

$
0
0

I use Bcrypt gem for password encryption.

User model:

class User < ApplicationRecord  has_secure_password  validates :password, presence: true, length: { minimum: 6 }, confirmation: true  # ...end

How to allow password to be blank when user is updated?:

expect(user.update(name: 'Joana', password: '')).to be(true)

But not update password?

expect { user.update(name: 'Joana', password: '') }.not_to(change { user.reload.password_digest })

If you want to test it, mind to find user before updating it not to get false positives:

user = User.find(user.id)

Viewing all articles
Browse latest Browse all 4619


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>