Rails 3.1プロジェクトでdeviseを使用しています。ユーザー用にrspecテストを個別に実行すると、問題なく渡されます。私はrake
を使用して、すべての私のテストを実行する場合すべてのテストを実行するときにencrypted_password requriedエラーが発生する
rspec spec/model/user_spec.rb
はしかし、それは
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
これらのテストは、ユーザーがGoogleやTwitterのいずれかを使用してサインアップしたときに焦点を当てるこれらの4つのユーザ関連のテストに失敗しました。
私はテストでデバッガを入れた場合、私は手動で私は、パスワードの入力を求められていますいくつかの理由について
(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
では、Googleのために、次のテストを追加しました。これらのユーザーがパスワードを必要としない
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のトップです:ユーザーに検証を見てみると
> 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}>]
Deviseは、直接 'encrypted_password'ではなく、' password'と 'password_confirmation'を検証します。 'user.send(:password_required?)。should be_true'テストを追加することができます。このテストに合格した場合は、その他のバリデーションが必要です。 – scorix
ユーザーがログインしているGoogleとtwitterのパスワードが間違っているはずです。私は私の質問でこのテストを更新しました。私は間違った場所で探しているように見える。何らかの理由で、すべてのテストを一緒に実行してもまだ検証に失敗しています。 – map7
あなたは、あなたのユーザモデルをさらに表示する必要があります、scorixによると、Deviseの 'encrypted_password'にはデフォルトのプレゼンスバリデータがありません。何かが追加されました(あなたか別の宝石のいずれか)。 –