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

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

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

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

Ruby on Rails

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

Q&A

0回答

790閲覧

railsでsystemspecでvisitが使えない

masaya-tn

総合スコア0

RSpec

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/03/18 07:38

前提・実現したいこと

railsアプリのテストを書いているのですが、ログイン周りのsystemspecでvisitを使いたいのですが、パスを指定してもそのパスが存在しないというエラーが出てしまいます。

visit new_user_session_path

Failures: 1) ログイン、ログアウト ログイン 認証情報が正しい場合 ログインできること Failure/Error: visit new_user_session_path NameError: undefined local variable or method `new_user_session_path' for #<RSpec::ExampleGroups::Nested::Nested::Nested:0x00007fcaf76a6610> # ./spec/system/user_session_spec.rb:10:in `block (4 levels) in <top (required)>' 2) ログイン、ログアウト ログイン 認証情報に誤りがある場合 ログインできないこと Failure/Error: visit new_user_session_path NameError: undefined local variable or method `new_user_session_path' for #<RSpec::ExampleGroups::Nested::Nested::Nested_2:0x00007fcaf76ad1e0> # ./spec/system/user_session_spec.rb:20:in `block (4 levels) in <top (required)>' 3) ログイン、ログアウト ログアウト ログアウトできること Failure/Error: visit new_user_session_path NameError: undefined local variable or method `new_user_session_path' for #<RSpec::ExampleGroups::Nested::Nested_2:0x00007fcaf76b4b48>

該当のソースコード

ruby

1require 'rails_helper' 2 3RSpec.describe 'ログイン、ログアウト', type: :system do 4 let(:user) { create(:user) } 5 6 describe 'ログイン' do 7 context '認証情報が正しい場合' do 8 it 'ログインできること' do 9 visit new_user_session_path 10 fill_in 'メールアドレス', with: user.email 11 fill_in 'パスワード', with: '12345678' 12 click_button 'ログイン' 13 expect(current_path).to eq outputs_path 14 expect(page).to have_content 'ログインしました' 15 end 16 end 17 18 context '認証情報に誤りがある場合' do 19 it 'ログインできないこと' do 20 visit new_user_session_path 21 fill_in 'メールアドレス', with: user.email 22 fill_in 'パスワード', with: '87654321' 23 click_button 'ログイン' 24 expect(current_path).to eq outputs_path 25 expect(page).to have_content 'ログインに失敗しました' 26 end 27 end 28 end 29 30 describe 'ログアウト' do 31 before do 32 login 33 end 34 it 'ログアウトできること' do 35 click_on('ログアウト') 36 expect(current_path).to eq root_path 37 expect(page).to have_content 'ログアウトしました' 38 end 39 end 40end 41

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', __FILE__) # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' require 'factory_bot' require 'capybara/rspec' ActiveRecord::Migration.maintain_test_schema! Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } 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 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") config.include FactoryBot::Syntax::Methods config.include SystemHelper, type: :system config.include Devise::Test::ControllerHelpers, type: :controller # config.include RequestSpecHelper, type: :request config.include Devise::Test::IntegrationHelpers, type: :system # config.before(:each, type: :system) do # driven_by :selenium, using: :headless_chrome, screen_size: [1920, 1080] # end end

spec_helper.rb

RSpec.configure do |config| # rspec-expectations config goes here. You can use an alternate # assertion/expectation library such as wrong or the stdlib/minitest # assertions if you prefer. config.expect_with :rspec do |expectations| # This option will default to `true` in RSpec 4. It makes the `description` # and `failure_message` of custom matchers include text for helper methods # defined using `chain`, e.g.: # be_bigger_than(2).and_smaller_than(4).description # # => "be bigger than 2 and smaller than 4" # ...rather than: # # => "be bigger than 2" expectations.include_chain_clauses_in_custom_matcher_descriptions = true end # rspec-mocks config goes here. You can use an alternate test double # library (such as bogus or mocha) by changing the `mock_with` option here. config.mock_with :rspec do |mocks| # Prevents you from mocking or stubbing a method that does not exist on # a real object. This is generally recommended, and will default to # `true` in RSpec 4. mocks.verify_partial_doubles = true end config.shared_context_metadata_behavior = :apply_to_host_groups # The settings below are suggested to provide a good initial experience # with RSpec, but feel free to customize to your heart's content. =begin # This allows you to limit a spec run to individual examples or groups # you care about by tagging them with `:focus` metadata. When nothing # is tagged with `:focus`, all examples get run. RSpec also provides # aliases for `it`, `describe`, and `context` that include `:focus` # metadata: `fit`, `fdescribe` and `fcontext`, respectively. config.filter_run_when_matching :focus # Allows RSpec to persist some state between runs in order to support # the `--only-failures` and `--next-failure` CLI options. We recommend # you configure your source control system to ignore this file. config.example_status_persistence_file_path = "spec/examples.txt" # Limits the available syntax to the non-monkey patched syntax that is # recommended. For more details, see: # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode config.disable_monkey_patching! # Many RSpec users commonly either run the entire suite or an individual # file, and it's useful to allow more verbose output when running an # individual spec file. if config.files_to_run.one? # Use the documentation formatter for detailed output, # unless a formatter has already been configured # (e.g. via a command-line flag). config.default_formatter = "doc" end # Print the 10 slowest examples and example groups at the # end of the spec run, to help surface which specs are running # particularly slow. config.profile_examples = 10 # Run specs in random order to surface order dependencies. If you find an # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 config.order = :random # Seed global randomization in this process using the `--seed` CLI option. # Setting this allows you to use `--seed` to deterministically reproduce # test failures related to randomization by passing the same `--seed` value # as the one that triggered the failure. Kernel.srand config.seed =end end

試したこと

エラーメッセージでググってみましたが同様の事例は見つかりませんでした

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

rails 5.2.3
ログイン機能はdeviseを使っています

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問