お世話になります。
RailsでSorceryを使ったユーザ認証を実装し、そのSpecを書こうとしているのですが、
Modelの検証の部分でつまづいております。
環境
- ruby(ruby 2.3.1p112) / rails (Rails 5.0.0.1)
- rspec-rails (3.5.2)
ruby
1 2# app/models/user.rb 3class User < ApplicationRecord 4 authenticates_with_sorcery! 5 6 validates :password, length: {minimum: 3 }, if: -> { new_record? || changes[:crypted_password]} 7 validates :password, confirmation: true, if: -> { new_record? || changes[:crypted_password]} 8 validates :password_confirmation, presence: true, if: -> { new_record? || changes[:crypted_password]} 9 10 validates :email, uniqueness: true 11end 12 13# app/controllers/user_controller.rb 14〜略〜 15def create 16 @user = User.new(user_params) 17 if @user.save 18 redirect_to @user, notice: 'User was successfully created !' 19 end 20end 21 22private 23def user_params 24 params.require(:user).permit(:email, :password, :password_confirmation) 25end 26 27 28# spec/models/user_spec.rb 29〜略〜 30context 'when match password and password_confirmation' do 31 it 'is valid' do 32 user1 = User.new(email: 'test@test.com', password: 'password', password_confirmation: 'password') 33 puts user1 #<= #<User id: nil, email: "test@test.com", crypted_password: nil, salt: nil, created_at: nil, updated_at: nil> となる 34 user1.valid? 35 expect(user1.errors[:password]).to include 'must be same' 36 end 37end 38
やりたいことは、「passwordとpassword_confirmationが同じ場合、検証エラーにならない(またはその逆)」の確認です。
上記のコードで登録はできるのですが、Rspec上だとpasswordとpassword_confirmationというパラメタではないので、どうやって検証すべきか分からずにおります。。
Sorceryを使ったRspecでのテストはいくつか記事が見つかったのですが、
モデルをテストしているものがなかったり、公式のこちらにもControllerの例しかありませんでした。
この場合、どう書けば良いのでしょうか。
アドバイスいただけると助かります。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。