ruby-on-rails - 验证失败 : Password digest can't be blank

标签 ruby-on-rails rspec

我见过其他线程有类似问题,但它们似乎没有解决我的问题。我正在关注 http://ruby.railstutorial.org/ 上的教程让一个基本的用户注册和登录或一个不同的项目然后那个示例项目。

基本上,当我运行 rspec 时,我不断收到此错误。我已经运行了所有迁移和测试准备。真的被这个难住了。

1) User should reject duplicate email addresses
 Failure/Error: User.create!(@attr)
 ActiveRecord::RecordInvalid:
   Validation failed: Password digest can't be blank, Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
 # ./spec/models/user_spec.rb:60:in `block (2 levels) in <top (required)>'

2) User should accept valid email addresses
 Failure/Error: valid_email_user.should be_valid end
   expected #<User id: nil, name: "Example User", email: "user@foo.com", created_at: nil, updated_at: nil, password_digest: nil> to be valid, but got errors: Password digest can't be blank, Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
 # ./spec/models/user_spec.rb:48:in `block (3 levels) in <top (required)>'
 # ./spec/models/user_spec.rb:46:in `each'
 # ./spec/models/user_spec.rb:46:in `block (2 levels) in <top (required)>'

3) User should create a new instance given valid attributes
 Failure/Error: User.create!(@attr)
 ActiveRecord::RecordInvalid:
   Validation failed: Password digest can't be blank, Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
 # ./spec/models/user_spec.rb:25:in `block (2 levels) in <top (required)>' 

4) User should reject email addresses identical up to case
 Failure/Error: User.create!(@attr.merge(:email => upcased_email))
 ActiveRecord::RecordInvalid:
   Validation failed: Password digest can't be blank, Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
 # ./spec/models/user_spec.rb:67:in `block (2 levels) in <top (required)>'

这是我的 user_spec
require 'spec_helper'

describe User do
  before(:each) do
    @attr = { :name => "Example User", :email => "user@example.com" }
  end

  before do
    @user = User.new(name: "Example User", email: "user@example.com",
                     password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:authenticate) }

  it { should be_valid }

  it "should create a new instance given valid attributes" do
    User.create!(@attr)
  end

  it "should require a name"  do
    no_name_user = User.new(@attr.merge(:name => ""))
    no_name_user.should_not be_valid
  end

  it "should require an email address" do
    no_email_user = User.new(@attr.merge(:email => ""))
    no_email_user.should_not be_valid
  end

  it "should reject names that are too long" do
    long_name = "a" * 51
    long_name_user = User.new(@attr.merge(:name => long_name))
    long_name_user.should_not be_valid
  end

  it "should accept valid email addresses" do
    addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
    addresses.each do |address|
      valid_email_user = User.new(@attr.merge(:email => address))
      valid_email_user.should be_valid end
  end

  it "should reject invalid email addresses" do
    addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
    addresses.each do |address|
      invalid_email_user = User.new(@attr.merge(:email => address))
      invalid_email_user.should_not be_valid end
  end

  it "should reject duplicate email addresses" do
    # Put a user with given email address into the database.
    User.create!(@attr)
    user_with_duplicate_email = User.new(@attr)
    user_with_duplicate_email.should_not be_valid
  end

  it "should reject email addresses identical up to case" do
    upcased_email = @attr[:email].upcase
    User.create!(@attr.merge(:email => upcased_email))
    user_with_duplicate_email = User.new(@attr)
    user_with_duplicate_email.should_not be_valid
  end

  describe "when password is not present" do
    before { @user.password = @user.password_confirmation = " " }
    it { should_not be_valid }
  end

  describe "when password doesn't match confirmation" do
    before { @user.password_confirmation = "mismatch" }
    it { should_not be_valid }
  end

  describe "when password confirmation is nil" do
    before { @user.password_confirmation = nil }
    it { should_not be_valid }
  end

  describe "with a password that's too short" do
    before { @user.password = @user.password_confirmation = "a" * 5 }
    it { should be_invalid }
  end

  describe "return value of authenticate method" do
    before { @user.save }
    let(:found_user) { User.find_by_email(@user.email) }

    describe "with valid password" do
      it { should == found_user.authenticate(@user.password) }
    end

    describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { should_not == user_for_invalid_password }
      specify { user_for_invalid_password.should be_false }
    end
  end

end

这是我的用户模型
class User < ActiveRecord::Base

  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
            format:     { with: VALID_EMAIL_REGEX },
            uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
end

最佳答案

你的验证也被破坏了,如果用户是创建的,但不是之后,应该需要密码和 password_confirmation

validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true

如果您稍后尝试更新用户,例如更改其名称,则会遇到问题,因为它会同时检查 password 和 password_confirmation 但用户已经有一个密码集,通常存储在由 bcrypt 加密但未存储的 password_digist 字段中以及密码和纯文本确认。

我建议仅对新记录进行验证:
validates :password, presence: true, length: { minimum: 6 } if new?
validates :password_confirmation, presence: true if new?

关于ruby-on-rails - 验证失败 : Password digest can't be blank,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17285208/

相关文章:

ruby-on-rails - scss 文件 rails 中的asset_path

ruby-on-rails - 我如何找到 Phusion Passenger 版本?

javascript - Rails 4 中的 js/css/images 插件放在哪里?

ruby-on-rails - Capybara 测试页面是否有正则表达式

ruby-on-rails - Rspec Controller 分配不按预期工作

javascript - 使用 AJAX 或 JS 刷新 Rails 部分

ruby-on-rails - API 调用使用 Net::HTTP 抛出 Net::HTTPBadResponse 错误

ruby-on-rails - Rspec 共享定义加载两次

ruby - 如何在 RSpec 中使用 `allow` 和 `let`

ruby-on-rails - 为什么 capybara 找不到表单元素?