アプリ内のフォロー機能をテストするためrspecで次のように記述しました
spec/system/follow_spec.rb require 'rails_helper' describe 'フォロー機能' do before do @follower = FactoryBot.create(:user, email: "sample@example.com", activated: true) @followed = FactoryBot.create(:user, activated: true) visit login_path fill_in "Email", with: "sample@example.com" fill_in "Password", with: "password" click_button "Log in" end it 'ユーザーをフォローする' do visit user_path(@followed) click_button 'Follow' expect(@follower.following).to include(@followed) end end
実行結果は以下のようにエラーとなります
フォロー機能 ユーザーをフォロー/フォロー解除する (FAILED - 1) Failures: 1) フォロー機能 ユーザーをフォロー/フォロー解除する Failure/Error: click_button 'Follow' Capybara::ElementNotFound: Unable to find button "Follow" that is not disabled [Screenshot]: /Users/sjira/environment/sample_app/tmp/screenshots/failures_r_spec_example_groups_nested_ユーザーをフォロー_フォロー解除する_918.png # ./spec/system/following_spec.rb:15:in `block (2 levels) in <top (required)>' Finished in 9.25 seconds (files took 0.78899 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/system/following_spec.rb:13 # フォロー機能 ユーザーをフォロー/フォロー解除する
スクリーンショットを見ると"visit user_path(@followed)"でユーザーページは開いていますがログインしていないために、フォローボタンが表示されていない状態でした(そのような仕様です)
一方でログイン機能のテストは通るので、ログインはできるけれど状態が維持されていないのだと思います
この原因を教えていただきたいです
ログイン機能のテスト、ログイン機能のコントローラは以下のとおりです
spec/system/login_spec.rb require 'rails_helper' describe 'ログイン機能', type: :system do before do @user = FactoryBot.create(:user, email: "sample@example.com") end describe '無効なユーザーでログイン' do before do visit login_path fill_in 'Email', with: 'sample@example.com' fill_in 'Password', with: 'password' click_button 'Log in' end it 'ログインページに戻される' do expect(page).to have_content "Log in" end end describe '有効なユーザー' do before do @user.toggle!(:activated) end context '誤ったパスワードでログイン' do before do visit login_path fill_in 'Email', with: 'sample@example.com' fill_in 'Password', with: 'foobar' click_button 'Log in' end it 'ログインページに戻される' do expect(page).to have_content "Log in" end end context '誤ったアドレスでログイン' do before do visit login_path fill_in 'Email', with: 'foo@bar.com' fill_in 'Password', with: 'password' click_button 'Log in' end it 'ログインページに戻される' do expect(page).to have_content 'Log in' end end context '正しいパスワード・アドレスでログイン' do before do visit login_path fill_in 'Email', with: 'sample@example.com' fill_in 'Password', with: 'password' click_button 'Log in' end it 'ユーザーページへ遷移' do expect(page).not_to have_content "Log in" expect(page).to have_content "Account" end end end end
app/controllers/sessions_controller.rb class SessionsController < ApplicationController def new end def create @user = User.find_by(email: params[:session][:email]) if @user&.authenticate(params[:session][:password].downcase) if @user.activated? log_in(@user) params[:session][:remember_me] == "1" ? remember(@user) : forget(@user) redirect_back_or(@user) else message = "Account not activated. " message += "Check your email for the activation link." flash[:warning] = message redirect_to root_url end else flash.now[:danger] = "invalid email/password combination" render "new" end end def destroy log_out if logged_in? redirect_to root_url end end
あなたの回答
tips
プレビュー