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

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

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

Capybaraは、 Rubyで開発されているWebアプリケーションテストフレームワークです。Webブラウザ不要でブラウザ上のユーザー操作及びJavaScriptの挙動を自動化することができます。

RSpec

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

Ruby on Rails

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

Q&A

解決済

1回答

6803閲覧

Rspec実行時にエラー An error occurred while loading spec_helper.

takumi0703

総合スコア5

Capybara

Capybaraは、 Rubyで開発されているWebアプリケーションテストフレームワークです。Webブラウザ不要でブラウザ上のユーザー操作及びJavaScriptの挙動を自動化することができます。

RSpec

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/04/08 03:50

前提・実現したいこと

Rspec,factory_bot_rails,capybaraを使用し、syetemspecのログインテストを再現したいです。

capybaraのドライバはselenium_chrome_headlessに設定しています。

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

bundle exec rspec spec/system/user_spec.rbを実行すると以下のエラーが出る。

terminal

1An error occurred while loading spec_helper. 2Failure/Error: reqiure 'capybara/rspec' 3 4NoMethodError: 5 undefined method `reqiure' for main:Object 6 Did you mean? require 7# ./spec/spec_helper.rb:18:in `<top (required)>' 8No examples found. 9 10 11Finished in 0.00003 seconds (files took 0.14022 seconds to load) 120 examples, 0 failures, 1 error occurred outside of examples

該当のソースコード

Gemfile

1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '2.6.3' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' 7gem 'rails', '~> 6.1.3' 8#Bootstrap 9gem 'bootstrap', '~> 4.3.1' 10gem 'jquery-rails' 11#日本語エラー 12gem 'rails-i18n' 13# Use sqlite3 as the database for Active Record 14gem 'sqlite3', group: [:development, :test] 15gem 'pg', group: :production 16# Use Puma as the app server 17gem 'puma', '~> 5.0' 18# Use SCSS for stylesheets 19gem 'sass-rails', '>= 6' 20# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 21gem 'webpacker', '~> 5.0' 22# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 23gem 'turbolinks', '~> 5' 24# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 25gem 'jbuilder', '~> 2.7' 26# Use Redis adapter to run Action Cable in production 27# gem 'redis', '~> 4.0' 28# Use Active Model has_secure_password 29gem 'bcrypt', '~> 3.1.7' 30 31# Use Active Storage variant 32# gem 'image_processing', '~> 1.2' 33 34# Reduces boot times through caching; required in config/boot.rb 35gem 'bootsnap', '>= 1.4.4', require: false 36 37 38#CarrierWave導入 39gem 'carrierwave' 40gem 'fog-aws' 41gem 'mini_magick' 42 43#開発環境とテスト環境 44group :development, :test do 45 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 46 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 47 gem 'rspec-rails', '~> 5.0.0' 48 gem 'factory_bot_rails' 49 gem 'capybara', '>= 3.26' 50 gem 'selenium-webdriver' 51end 52 53group :development do 54 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 55 gem 'web-console', '>= 4.1.0' 56 # Display performance information such as SQL time and flame graphs for each request in your browser. 57 # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md 58 gem 'rack-mini-profiler', '~> 2.0' 59 gem 'listen', '~> 3.3' 60 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 61 gem 'spring' 62end 63 64group :test do 65 # Adds support for Capybara system testing and selenium driver 66 67 # Easy installation and use of web drivers to run system tests with browsers 68 gem 'webdrivers' 69 70end 71 72# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 73gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 74

spec/spec_helper.rb

# This file was generated by the `rails generate rspec:install` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause # this file to always be loaded, without a need to explicitly require it in any # files. # # Given that it is always loaded, you are encouraged to keep this file as # light-weight as possible. Requiring heavyweight dependencies from this file # will add to the boot time of your test suite on EVERY test run, even for an # individual file that may not need all of that loaded. Instead, consider making # a separate helper file that requires the additional dependencies and performs # the additional setup, and require it from the spec files that actually need # it. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration #Rspecでcapybaraを扱うための読み込み reqiure 'capybara/rspec' RSpec.configure do |config| #Systemspecを実行するドライバの設定 config.before(:each, type: :system) do driven_by :selenium_chrome_headless end # 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 # This option will default to `:apply_to_host_groups` in RSpec 4 (and will # have no way to turn it off -- the option exists only for backwards # compatibility in RSpec 3). It causes shared context metadata to be # inherited by the metadata hash of host groups and examples, rather than # triggering implicit auto-inclusion in groups with matching metadata. 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

spec/system/user_spec.rb

require 'rails_helper' describe 'ログイン成功テスト',type: :system do describe'user詳細画面が表示される' do #仮ユーザー作成 before do user_a =FactoryBot.create(:user,name:'ユーザーA',email:'a@example.com') end context 'ログインしている時の操作' do before do #入力された値 visit: login_path fill_in: 'メールアドレス',with: 'a@example.com' fill_in: 'パスワード',with: 'password' click_botton: 'ログイン' end it '正しい値が設定してあればログインは成功する' do expect(@user.valid?).to eq(true) end end end end

spec/factories/user.rb

FactoryBot.define do factroy :user do name('テストユーザー') email('test@example.com') password('password') end end

試したこと

capybaraの読み込みがうまくいっていないのかと思い

RSpec.configure do |config| #Systemspecを実行するドライバの設定 config.before(:each, type: :system) do config.include Capybara::DSL ←追記 driven_by :selenium_chrome_headless end

DSLで強制読み込みしようと試みましたが、エラー内容が変わりませんでした。

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

MacOS 10.15.7

Rspecをまだ触りたてで不足部分が多々あると思いますが、ご教授のほどよろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

spec/spec_helper.rb で require が reqiure になってます。

ruby

1reqiure 'capybara/rspec'

ruby

1require 'capybara/rspec'

投稿2021/04/08 06:52

neko_daisuki

総合スコア2090

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

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

takumi0703

2021/04/08 07:08

申し訳ありません。ケアレスミスでした。 ご指摘ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問