【发布时间】:2016-10-14 15:24:48
【问题描述】:
我在 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,但我仍然会收到错误。
更新
为谷歌注册用户添加了以下测试
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}>]
【问题讨论】:
-
设计只验证
password和password_confirmation,而不是直接验证encrypted_password。您可以添加测试user.send(:password_required?).should be_true。如果此测试通过,则必须进行一些其他验证。 -
password_required 对于 google 和 twitter 登录用户来说应该是 false。我已经在我的问题中更新了这个测试。所以看起来我一直在寻找错误的地方。出于某种原因,在同时运行所有测试时仍然无法通过验证。
-
你需要展示更多你的用户模型,就像 scorix 说的那样,Devise 的
encrypted_password上没有默认的存在验证器。一些东西加了一个(你或另一个宝石)。 -
从代码中猜测您正在为 google 和 twitter 身份验证实现omniauth。检查gist.github.com/kinopyo/1338738
标签: ruby-on-rails ruby rspec devise