2016-06-14 66 views
0

我在Rails 3.1项目中使用devise,当我单独为用户运行rspec测试时,它们没有问题。在运行所有测试时设计encrypted_pa​​ssword requried错误

rspec spec/model/user_spec.rb 

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

describe "validation" do 
    context "when user is from google" do 
     before do 
     @user = User.new(email: "[email protected]") 
     @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 

这些测试的重点当用户注册使用无论是谷歌或Twitter上。

如果我把一个调试器在测试中我可以手动键入

(rdb:1) @user.errors 
#<ActiveModel::Errors:0x0000000ca98e98 @base=#<User id: nil, email: "[email protected]", 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: "[email protected]", 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,但我仍然得到错误。

UPDATE

增加了以下测试谷歌签署了用户

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

这些用户不需要密码。当我运行user_spec时,以及当我使用rake一起运行它时,此测试通过。其他测试在使用rake运行时仍然失败,因为它们仍未通过验证。

UPDATE

这里是我的用户模型的顶部

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> 

UPDATE:在属性encrypted_password上的用户望着验证

> 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[^@][email protected]([^@\.]+\.)+[^@\.]+\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}>] 
+0

设计只直接验证'password'和'password_confirmation',而不''encrypted_pa​​ssword'。你可以添加一个测试'user.send(:password_required?)。should be_true'。如果此测试通过,则必须有其他验证。 – scorix

+0

对于在用户中登录的Google和Twitter,password_required应该为false。我已经在我的问题中更新了这个测试。所以看起来我一直在寻找错误的地方。由于某些原因,它们在一起运行所有测试时仍然失败。 – map7

+0

您需要展示更多的用户模型,比如scorix说,Devise的'encrypted_pa​​ssword'上没有默认的在线验证器。东西加了一个(无论你还是另一个宝石)。 –

回答

0

必须有一个PresenceValidator

运行此控制台列出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) 

查找验证并删除它。

相关问题