RSpecに失敗します。from現場rails

前提

現在「現場で使えるRuby on rails5速習実践ガイド」という書籍を参考にしながらタスク管理アプリを作成しております。
教材を参考にコードを書き、bundle spec rspec spec/system/tasks_spec.rbを実行したところ、以下のエラーが発生しました。

発生している問題・エラーメッセージ

DEBUGGER: Attaching after process 97910 fork to child process 97932 ..F Failures: 1) タスク管理機能 詳細表示機能 ユーザーAがログインしているとき ユーザーAが作成したタスクが表示される Failure/Error: expect(page).to have_content '最初のタスク' expected to find text "最初のタスク" in "Taskleaf\nログイン\nログイン\nメールアドレス\nパスワード" [Screenshot Image]: /Users/sampleuser/taskleaf/tmp/capybara/failures_r_spec_example_groups_nested_nested_2_a_ユーザーaが作成したタスクが表示される_935.png # ./spec/system/tasks_spec.rb:42:in `block (4 levels) in <top (required)>' Finished in 9.74 seconds (files took 2.02 seconds to load) 3 examples, 1 failure Failed examples: rspec ./spec/system/tasks_spec.rb:41 # タスク管理機能 詳細表示機能 ユーザーAがログインしているとき ユーザーAが作成したタスクが表示される

spec/system/tasks_spec.rb

require 'rails_helper' describe 'タスク管理機能', type: :system do let(:user_a) { FactoryBot.create(:user, name: 'ユーザーA', email: 'a@example.com') } let(:user_b) { FactoryBot.create(:user, name: 'ユーザーB', email: 'b@example.com') } let!(:task_a) { FactoryBot.create(:task, name: '最初のタスク', user: user_a) } before do visit login_path fill_in 'メールアドレス', with: login_user.email fill_in 'パスワード', with: login_user.password click_button 'ログインする' end describe '一覧表示機能' do context 'ユーザーAがログインしているとき' do let(:login_user) { user_a } it 'ユーザーAが作成したタスクが表示される' do expect(page).to have_content '最初のタスク' end end context 'ユーザーBがログインしているとき' do let(:login_user) { user_b } it 'ユーザーAが表示したタスクが表示されない' do expect(page).to have_no_content '最初のタスク' end end end describe '詳細表示機能' do context 'ユーザーAがログインしているとき' do let(:login_user) { user_a } before do visit task_path(task_a) end it 'ユーザーAが作成したタスクが表示される' do expect(page).to have_content '最初のタスク' end end end end

本来task_path(task_a)に遷移したいのですが、なぜかloginページに遷移して、テストが失敗します。
確かに、ログインしていないとloginページにリダイレクトされるようにしていますが、user_aでログインが成功しているのに何故??っていう感じです・・・。

該当のソースコード

application_controller.rb

class ApplicationController < ActionController::Base helper_method :current_user before_action :login_required private def current_user @current_user ||= User.find_by(id: session[:user_id]) if session[:user_id] end def login_required redirect_to login_url unless current_user end end

補足情報(FW/ツールのバージョンなど)

rails 7.0.3.1
rspec 3.11

コメントを投稿

0 コメント