前提・実現したいこと
現場で使えるRubyonRails5速習実践ガイドをDocker環境で学習しています。
Rspecでの検証において、visit task_path(task_a)
でエラーが出ました。
NameError:undefined local variable or method 'task_a' for #<RSpec::ExampleGroups::Nested::Nested_2::A:0x000055f5b4270a60>
調べてみると、spec_helper.rbに以下を追加すると解決するそうなのですが、次はRails.application.routes.url_helpers
が読み込めないというエラーが発生してしまいます。
RSpec.configure do |config| config.include Rails.application.routes.url_helpers end
参考
stackoverflow「Rspec and named routes」
エラーメッセージ
An error occurred while loading spec_helper. Failure/Error: config.include Rails.application.routes.url_helpers NameError: uninitialized constant Rails # ./spec/spec_helper.rb:19:in `block in <top (required)>' # ./spec/spec_helper.rb:18:in `<top (required)>' An error occurred while loading ./spec/system/tasks_spec.rb. Failure/Error: config.include Rails.application.routes.url_helpers NameError: uninitialized constant Rails # ./spec/spec_helper.rb:19:in `block in <top (required)>' # ./spec/spec_helper.rb:18:in `<top (required)>' # ./spec/rails_helper.rb:2:in `require' # ./spec/rails_helper.rb:2:in `<top (required)>' # ./spec/system/tasks_spec.rb:1:in `require' # ./spec/system/tasks_spec.rb:1:in `<top (required)>' No examples found.
該当のソースコード
spec/system/tasks_spec.rb
require 'rails_helper' describe 'タスク管理機能',type: :system do describe '一覧表示機能' 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 FactoryBot.create(:task, name: '最初のタスク', user: user_a) visit login_path fill_in 'メールアドレス', with: login_user.email fill_in 'パスワード', with: login_user.password click_button 'ログインする' end 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 # ユーザーAが作成したタスクが画面上に表示されないことを確認 expect(page).not_to have_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
spec/spec_helper.rb
ruby
1require 'capybara/rspec' 2 3RSpec.configure do |config| 4 config.include Rails.application.routes.url_helpers 5 config.before(:each, type: :system) do 6 driven_by :selenium_chrome_headless 7 end 8 9 config.expect_with :rspec do |expectations| 10 expectations.include_chain_clauses_in_custom_matcher_descriptions = true 11 end 12 13 config.mock_with :rspec do |mocks| 14 mocks.verify_partial_doubles = true 15 end 16 17 config.shared_context_metadata_behavior = :apply_to_host_groups 18 19end 20
spec/rails_helper.rb
require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__) abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' begin ActiveRecord::Migration.maintain_test_schema! rescue ActiveRecord::PendingMigrationError => e puts e.to_s.strip exit 1 end RSpec.configure do |config| config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = true config.infer_spec_type_from_file_location! config.filter_rails_from_backtrace! end
Gemfile
source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.1.4' # Use postgresql as the database for Active Record gem 'pg', '~> 0.18' # Use Puma as the app server gem 'puma', '~> 3.7' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 3.0' # Use ActiveModel has_secure_password gem 'bcrypt', '~> 3.1.7' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] gem 'factory_bot_rails', '~> 4.11' # Adds support for Capybara system testing and selenium driver gem 'capybara', '~> 2.13' gem 'selenium-webdriver' gem 'webdrivers', '~> 3.0' gem 'rspec-rails', '~> 3.7' gem 'rails-controller-testing' end group :development do # Access an IRB console on exception pages or by using <%= console %> anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] # for slim gem 'slim-rails' gem 'html2slim' gem 'bootstrap' gem 'rails_autolink'
補足情報(FW/ツールのバージョンなど)
- Docker version 18.09.2
- docker-compose version 1.23.2
- ruby 2.5.5
- Rails 5.2.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/07 00:46
2019/07/07 01:02