質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Q&A

解決済

1回答

1345閲覧

Rpecのヘルパーメソッドを共通化したい。

divclass123

総合スコア35

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

0グッド

0クリップ

投稿2022/04/30 06:22

すみません、すごく初歩的な質問になってしまうのですが、specのヘルパーメソッドを共通化したいのですがそのやり方がわかりません。

エラーメッセージ

ruby

1An error occurred while loading ./spec/system/sessions_spec.rb. 2Failure/Error: 3 RSpec.describe SessionsHelper, type: :helper do 4 def is_logged_in? 5 !session[:user_id].nil? 6 end 7 end 8 9NameError: 10 uninitialized constant SessionsHelper 11# ./spec/helpers/sessions_helper_spec.rb:13:in `<top (required)>' 12# ./spec/rails_helper.rb:3:in `require' 13# ./spec/rails_helper.rb:3:in `<top (required)>' 14# ./spec/system/sessions_spec.rb:1:in `require' 15# ./spec/system/sessions_spec.rb:1:in `<top (required)>' 16Run options: include {:locations=>{"./spec/system/sessions_spec.rb"=>[4]}} 17 18All examples were filtered out 19 20Finished in 0.00003 seconds (files took 0.87982 seconds to load) 210 examples, 0 failures, 1 error occurred outside of examples 22

該当ファイル

spec/helpers/sessions_helper_spec.rb

このメソッドを共通化したいです。

ruby

1require 'rails_helper' 2 3# Specs in this file have access to a helper object that includes 4# the SessionsHelper. For example: 5# 6# describe SessionsHelper do 7# describe "string concat" do 8# it "concats two strings with spaces" do 9# expect(helper.concat_strings("this","that")).to eq("this that") 10# end 11# end 12# end 13RSpec.describe SessionsHelper, type: :helper do 14 def is_logged_in? 15 !session[:user_id].nil? 16 end 17end 18

spec/rails_helper.rb

ヘルパーメソッドを定義したファイルをrequireすればいいのではと思いました。

ruby

1# This file is copied to spec/ when you run 'rails generate rspec:install' 2require 'spec_helper' 3require './spec/helpers/sessions_helper_spec' 4# ↑これで共通化できると思いました。

spec/system/sessions_spec.rb

ruby

1 2require 'rails_helper' 3 4RSpec.describe "Sessions", type: :system do 5 context "ユーザー登録した場合" do 6 let!(:user) { create(:user) } 7 8 it "ログインできる" do 9 visit login_path 10 fill_in 'session[email]', with: user.email 11 fill_in 'session[password]', with: user.password 12 click_on 'ログイン' 13 expect(page).to have_current_path("/users/#{user.id}") 14 expect(page).to have_content(user.name) 15 expect(page).to have_link "ログアウト", href: "/logout" 16 expect(is_logged_in?).eq true #<= ここでエラーが起こりました。 17 end 18 end 19

自分の考え

ためしに

require './spec/helpers/sessions_helper_spec'
をコメントアウトすると

ruby

1 2# This file is copied to spec/ when you run 'rails generate rspec:install' 3require 'spec_helper' 4# require './spec/helpers/sessions_helper_spec'

undifined method 'is_logged_in?'
となりました。
なので、コメントアウトせずに

ruby

1 2# This file is copied to spec/ when you run 'rails generate rspec:install' 3require 'spec_helper' 4require './spec/helpers/sessions_helper_spec'

と記述することで'./spec/helpers/sessions_helper_spec'の読み込み自体は成功してる気がします。
言い換えるとパスはあってる気がします。

sessionモデルは作成してないのでそれが原因でエラーが起きてるかもしれないですが、
モデルを作成せずヘルパーメソッドを共通化したい場合どうすればいいのか分かりませんでした。
ご回答いただければ幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

この記事を参考に

spec/support/helpers/sessions_helper.rb

を作成

ruby

1 2module RSpecSessionsHelpers 3 def is_logged_in? 4 !session[:user_id].nil? 5 end 6end 7

そのモジュールをspec/rails_helperに読み込み

spec/rails_helper.rb

ruby

1 2require 'support/helpers/sessions_helper' 3 4Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } 5 6RSpec.configure do |config| 7 config.include RspecSessionsHelper 8end

投稿2022/04/30 07:59

divclass123

総合スコア35

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問