RSpec - 安装Devise gem后密码不能出现空白错误

时间:2014-07-27 19:01:53

标签: ruby-on-rails ruby rspec devise

我测试用户模型:

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我有&#34;密码不能为空?&#34;错误enter image description here

什么可能导致&#34;密码不能为空?&#34;错误?

提前致谢。

1 个答案:

答案 0 :(得分:0)

@jvnil的建议是对的。正如您在设计代码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)
相关问题