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

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

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

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

RSpec

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

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

Q&A

解決済

1回答

4385閲覧

『Ruby on Rails 5 速習実践ガイド』のRSpecで「visit 〇〇_path」が使用できない

runes224

総合スコア14

Ruby on Rails 5

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

RSpec

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

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

0グッド

1クリップ

投稿2019/07/04 21:26

編集2019/07/06 01:16

前提・実現したいこと

現場で使える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

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

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

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

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

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

guest

回答1

0

ベストアンサー

こんばんは。
以下の部分を実行する際に、task_aという変数やメソッドなんて無いよ!と言われていますよね。

specの最初の方で、let!(:task_a) { ... } としているものの、これは describe '一覧表示機能' do ... end の中でしか適用されないと思います。
(describeの入れ子になっていますが、範囲をよく確認してみてくださいね)

ほかにも方法はあるかと思いますが、letおよびlet!の部分を、describe 'タスク管理機能',type: :system do の直下に持っていけばいいのではないでしょうか。

(spec_helper.rbの調整は多分しなくて大丈夫かなと思います)

投稿2019/07/06 15:40

suama

総合スコア1997

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

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

runes224

2019/07/07 00:46

単純なミスだったのですね。ありがとうございました!
suama

2019/07/07 01:02

動かしてないのでわかりませんが、大丈夫でしたらよいですが^^
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問