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

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

新規登録して質問してみよう
ただいま回答率
85.49%
RSpec

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

Ruby on Rails

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

Q&A

解決済

1回答

755閲覧

Rspecで書いてるlog_in_asのテストエラー

popi06

総合スコア3

RSpec

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/03/07 15:16

編集2021/03/11 04:41

前提・実現したいこと

rails tutorial 9章を真似たlog_in_asのテストをRspecで書いていたのですが、以下のエラーが発生しました。ネットでかなり調べたのですが、色々試したものの解決しなかったので質問させていただきます。

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

1) UsersLogin 登録情報 チェックすると、Cookieが記憶される Failure/Error: log_in_as(@user,remember_me: '1') NoMethodError: undefined method `log_in_as' for #<RSpec::ExampleGroups::UsersLogin::Nested:0x000055b9e764ee88> Did you mean? login_path # ./spec/features/users_login_spec.rb:29:in `block (3 levels) in <main>' 2) UsersLogin 登録情報 チェックしない場合、Cookieを記憶しない Failure/Error: log_in_as(@user,remember_me: '0') NoMethodError: undefined method `log_in_as' for #<RSpec::ExampleGroups::UsersLogin::Nested:0x000055b9e6ff7b70> Did you mean? login_path # ./spec/features/users_login_spec.rb:33:in `block (3 levels) in <main>'

ソースコード

/spec/support/test_helper.rb ENV['RAILS_ENV'] ||= 'test' require_relative '../config/environment' require 'rails/test_help' class ActiveSupport::TestCase # Run tests in parallel with specified workers parallelize(workers: :number_of_processors) # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all # Add more helper methods to be used by all tests here... def is_logged_in? !session[:user_id].nil? end def log_in_as(user) session[:user_id] = user.id end def log_in_as(user) visit login_path fill_in 'メールアドレス', with: user.email fill_in 'パスワード', with: user.password click_button 'ログイン' end end
/spec/features/users_login_spec.rb require 'rails_helper' RSpec.feature "UsersLogin", type: :feature do before do @user = FactoryBot.create(:user) end ... describe '登録情報' do it "チェックすると、Cookieが記憶される" do log_in_as(@user,remember_me: '1') expect(cookies[:remember_token]).not_to eq nil end it "チェックしない場合、Cookieを記憶しない" do log_in_as(@user,remember_me: '0') expect(cookies[:remember_token]).to eq nil end end end
/spec/rails_helper.rb # This file is copied to spec/ when you run 'rails generate rspec:install' require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../config/environment', __dir__) # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end # in _spec.rb will both be required and run as specs, causing the specs to be # run twice. It is recommended that you do not name files matching this glob to # end with _spec.rb. You can configure this pattern with the --pattern # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. # # The following line is provided for convenience purposes. It has the downside # of increasing the boot-up time by auto-requiring all files in the support # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # # Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f } # Checks for pending migrations and applies them before tests are run. # If you are not using ActiveRecord, you can remove these lines. begin ActiveRecord::Migration.maintain_test_schema! rescue ActiveRecord::PendingMigrationError => e puts e.to_s.strip exit 1 end RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. config.use_transactional_fixtures = true # You can uncomment this line to turn off ActiveRecord support entirely. # config.use_active_record = false # RSpec Rails can automatically mix in different behaviours to your tests # based on their file location, for example enabling you to call `get` and # `post` in specs under `spec/controllers`. # # You can disable this behaviour by removing the line below, and instead # explicitly tag your specs with their type, e.g.: # # RSpec.describe UsersController, type: :controller do # # ... # end # # The different available types are documented in the features, such as in # https://relishapp.com/rspec/rspec-rails/docs config.infer_spec_type_from_file_location! # Filter lines from Rails gems in backtraces. config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") Shoulda::Matchers.configure do |config| config.integrate do |with| with.test_framework :rspec with.library :rails end end end

試したこと

Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }をコメントインしてみましたが、別のエラーが出てしまいます。

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

ちなみに試したことの部分を実行すると下のエラーが出ます。

An error occurred while loading rails_helper. Failure/Error: require_relative '../config/environment' LoadError: cannot load such file -- /home/ubuntu/environment/アプリ名/spec/config/environment

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

自己解決

test_helperの不要なコードを削除し、log_in_asのpostアクションを違うコントローラーに書き換えた結果、エラーが解決しました。

投稿2021/03/11 14:54

popi06

総合スコア3

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問