繁体   English   中英

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

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

我测试用户模型:

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

用户架构如下:

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

工厂是:

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代码为:

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

运行rspec我有“密码不能为空”错误 在此处输入图片说明

什么会导致“密码不能为空”错误?

提前致谢。

@jvnil的建议是正确的。 如您在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

并在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM