运行所有测试时设计encrypted_pa​​ssword requried错误

时间:2016-06-14 04:07:24

标签: ruby-on-rails ruby rspec devise

我在Rails 3.1项目中使用设计,当我单独为用户运行我的rspec测试时,它们没有任何问题。

rspec spec/model/user_spec.rb

但是,如果我使用rake运行所有测试,那么这四个用户相关测试就会失败

  describe "validation" do
    context "when user is from google" do
      before do
        @user = User.new(email: "example@gmail.com")
        @user.authentications.build(provider: "google", uid: "1234")
      end

      it "should be valid" do
        @user.valid?.should be_true
      end

      it "should save" do
        @user.save.should be_true
      end
    end

    context "when user is from twitter" do
      before do
        @user = User.new(email: nil)
        @user.authentications.build(provider: "twitter", uid: "1234")
      end

      it "should be valid" do
        @user.valid?.should be_true
      end

      it "should save" do
        @user.save.should be_true
      end
    end
  end

这些测试重点关注用户使用Google或Twitter注册的时间。

如果我在测试中放置调试器,我可以手动输入

(rdb:1) @user.errors
#<ActiveModel::Errors:0x0000000ca98e98 @base=#<User id: nil, email: "example@gmail.com", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, admin: nil, given_names: nil, surname: nil, dob: nil, address: nil, suburb: nil, state: nil, country: nil, phone: nil, interests: nil, venue_manager: nil, nickname: nil, data_entry: nil>, @messages={}>
(rdb:1) @user.valid?
false
(rdb:1) @user.errors
#<ActiveModel::Errors:0x0000000ca98e98 @base=#<User id: nil, email: "example@gmail.com", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, admin: nil, given_names: nil, surname: nil, dob: nil, address: nil, suburb: nil, state: nil, country: nil, phone: nil, interests: nil, venue_manager: nil, nickname: nil, data_entry: nil>, @messages={:encrypted_password=>["can't be blank"]}>
(rdb:1) 

出于某种原因,我被提示输入密码。我的用户模型中有以下方法。

  def password_required?
    authentications.empty? && (!persisted? || password.present? || password_confirmation.present?)
  end

如果我将调试器放在那里,那么我可以证明这将返回false,但我仍然会收到错误。

更新

为Google注册用户添加了以下测试

  it "should not require a password" do
    @user.send(:password_required?).should be_false
  end

这些用户不需要密码。当我单独运行user_spec和使用rake一起运行它时,此测试都会通过。使用rake运行时,其他测试仍然失败,因为它们仍然无法通过验证。

更新

这是我的用户模型的顶部

class User < ActiveRecord::Base
  has_many :authentications    
  has_many :created_venues, foreign_key: :created_by_id, class_name: "Venue"
  has_many :manages
  has_many :venues, :through => :manages    
  has_many :events

  devise :database_authenticatable, :registerable, 
         :recoverable, :rememberable, :trackable, :validatable

  default_scope :order => "given_names"

  # Setup accessible (or protected) attributes for your model
  attr_protected :admin
  attr_reader :venue_tokens
  <snip>

更新:查看用户

上的验证
 > p User._validate_callbacks.map(&:raw_filter)
[:validate_associated_records_for_authentications, :validate_associated_records_for_created_venues, :validate_associated_records_for_manages, :validate_associated_records_for_venues, :validate_associated_records_for_events, #<ActiveModel::Validations::PresenceValidator:0x000000087a54b0 @attributes=[:email], @options={:if=>:email_required?}>, #<ActiveRecord::Validations::UniquenessValidator:0x000000089a5260 @attributes=[:email], @options={:case_sensitive=>true, :allow_blank=>true, :if=>:email_changed?}, @klass=User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, admin: boolean, given_names: string, surname: string, dob: date, address: string, suburb: string, state: string, country: string, phone: string, interests: text, venue_manager: boolean, nickname: string, data_entry: boolean)>, #<ActiveModel::Validations::FormatValidator:0x000000089fe518 @attributes=[:email], @options={:with=>/\A[^@]+@([^@\.]+\.)+[^@\.]+\z/, :allow_blank=>true, :if=>:email_changed?}>, #<ActiveModel::Validations::PresenceValidator:0x00000008a874f8 @attributes=[:password], @options={:if=>:password_required?}>, #<ActiveModel::Validations::ConfirmationValidator:0x00000008abb5c8 @attributes=[:password], @options={:if=>:password_required?}>, #<ActiveModel::Validations::LengthValidator:0x00000008ae66d8 @attributes=[:password], @options={:allow_blank=>true, :minimum=>6, :maximum=>128}>]

1 个答案:

答案 0 :(得分:0)

属性PresenceValidator上必须有encrypted_password

在控制台中运行此命令以列出User型号的所有验证回调:

# active_record 4
p User.send(:get_callbacks, :validate).instance_variable_get(:@chain).map(&:filter)

# active_record 3
p User._validate_callbacks.map(&:raw_filter)

找到验证器并将其删除。