简体   繁体   English

RSpec-安装Devise gem后密码不能为空错误

[英]RSpec - Password can't be blank error after installing Devise gem

I test User model: 我测试用户模型:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :password, :presence => true

  has_many :ads,             dependent: :destroy
  has_many :docs,            through: :ads, source: :content, source_type: "Doc"
  has_many :pets,            through: :ads, source: :content, source_type: "Pet"
  has_many :license_plates,  through: :ads, source: :content, source_type:                 "LicensePlate"
end

users schema is following: 用户架构如下:

create_table "users", force: true do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.string   "phone"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
  end

Factory is: 工厂是:

FactoryGirl.define do
  factory :user do
    first_name { Faker::Name.first_name }
    last_name { Faker::Name.last_name }
    phone { Faker::PhoneNumber.phone_number }
    email { Faker::Internet.email }
    encrypted_password { Faker::Internet.password }
    #reset_password_token
  end
end

RSpec code is: RSpec代码为:

require 'rails_helper'
require 'spec_helper'

describe User do

  it "has a valid factory" do

    expect(FactoryGirl.build(:user)).to be_valid

  end

  it { is_expected.to have_many(:ads).dependent(:destroy) }
  it { is_expected.to have_many(:docs).through(:ads) }
  it { is_expected.to have_many(:pets).through(:ads) }
  it { is_expected.to have_many(:license_plates).through(:ads) }

end

Running rspec I have "Password can't be blank" error 运行rspec我有“密码不能为空”错误 在此处输入图片说明

What can cause "Password can't be blank" error? 什么会导致“密码不能为空”错误?

Thanks in advance. 提前致谢。

The suggestion of @jvnil is right. @jvnil的建议是正确的。 As you can see in the Devise code https://github.com/plataformatec/devise/blob/master/lib/devise/models/database_authenticatable.rb 如您在Devise代码中所见, https://github.com/plataformatec/devise/blob/master/lib/devise/models/database_authenticatable.rb

  # Verifies whether an password (ie from sign in) is the user password.
  def valid_password?(password)
    return false if encrypted_password.blank?
    bcrypt   = ::BCrypt::Password.new(encrypted_password)
    password = ::BCrypt::Engine.hash_secret("#{password}#{self.class.pepper}", bcrypt.salt)
    Devise.secure_compare(password, encrypted_password)
  end

#

# Generates password encryption based on the given value.
      def password=(new_password)
        @password = new_password
        self.encrypted_password = password_digest(@password) if @password.present?
      end

And validates that at https://github.com/plataformatec/devise/blob/master/lib/devise/strategies/database_authenticatable.rb 并在https://github.com/plataformatec/devise/blob/master/lib/devise/strategies/database_authenticatable.rb上进行验证

resource  = valid_password? && mapping.to.find_for_database_authentication(authentication_hash)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 安装devise gem时出错 - Error installing devise gem Rails-设计错误:创建新用户时,“电子邮件不能为空,密码不能为空” - Rails - Devise error: “Email can't be blank, password can't be blank” when create new user 设计重置密码令牌不能为空 - Devise Reset password token can't be blank Rails - Devise Invitable - 两个 Devise 用户模型 - 在发送邀请之前密码不能为空错误 devise - Rails - Devise Invitable - Two Devise User Models - Password can't be blank error with devise prior to invitation being sent 如果仅密码确认为空白,为什么Rails dev gem会抛出“无效的当前密码”错误? - Why rails devise gem throws 'invalid current password' error if only password confirmation is blank? 设计错误:电子邮件不能为空 - Devise error: email can't be blank 更新设计帐号时当前密码不能为空 - Current password can't be blank when updating devise account Devise在API请求中说“密码不能为空”-Rails API - Devise says 'Password Can't be Blank' on a API request - Rails API 如果在dev gem中当前密码为空,如何更新密码? - How to update password if current password is blank in devise gem? 设计的Rspec:无法批量分配受保护的属性:user_password,user_password_confirmation - Rspec for Devise: Can't mass-assign protected attributes: user_password, user_password_confirmation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM