[英]devise encrypted_password requried error when running all tests
我在Rails 3.1项目上使用devise,当我为用户单独运行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}>]
必须有一个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)
找到验证器并将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.