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

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

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

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Q&A

解決済

1回答

671閲覧

テスト実行するもエラーが発生します。。

mmmaaarrrkkk000

総合スコア16

Ruby on Rails 5

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/09/16 06:11

質問内容・実現したいこと

ステータス、「下書き」「公開」「公開待ち」の「公開待ちに」に変更して「記事が公開待ちに変更しました」のフラッシュメッセージが表示されることを確認するテストを書いています。

bundle exec rspec spec/system/admin_articles_spec.rb を実行後、下記のエラーになります。

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

bundle exec rspec spec/system/admin_articles_spec.rb An error occurred while loading ./spec/system/admin_articles_spec.rb. Failure/Error: require File.expand_path('../config/environment', __dir__) NameError: undefined local variable or method `x' for main:Object # ./config/initializers/simple_form_bootstrap.rb:1:in `<top (required)>' # ./vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:285:in `load' # ./vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:285:in `block in load' # ./vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency' # ./vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:285:in `load' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:657:in `block in load_config_initializer' # ./vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/notifications.rb:170:in `instrument' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:656:in `load_config_initializer' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:614:in `block (2 levels) in <class:Engine>' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:613:in `each' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:613:in `block in <class:Engine>' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `instance_exec' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `run' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:61:in `block in run_initializers' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:50:in `each' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:50:in `tsort_each_child' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:60:in `run_initializers' # ./vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/application.rb:361:in `initialize!' # ./config/environment.rb:5:in `<top (required)>' # ./spec/rails_helper.rb:5:in `require' # ./spec/rails_helper.rb:5:in `<top (required)>' # ./spec/system/admin_articles_spec.rb:1:in `require' # ./spec/system/admin_articles_spec.rb:1:in `<top (required)>' No examples found. Finished in 0.00007 seconds (files took 31.36 seconds to load) 0 examples, 0 failures, 1 error occurred outside of examples

該当のソースコード

spec/system/admin_articles_spec.rb

ruby

1describe '記事の編集画面' do 2 context '公開日時を未来に設定して「公開」を押す' do 3 it 'ステータスを「公開待ち」に変更して「記事が公開待ちに変更しました」のフラッシュメッセージが表示されること' do 4 visit edit_admin_article_path(article_future.uuid) 5 click_on '公開する' 6 expect(page).to have_content('記事を公開待ちにしました') 7 expect(page).to have_select('状態', selected: '公開待ち'), 'ステータスが「公開待ち」になっていません' 8 end 9 end 10 end

rails_helper.rb

ruby

1# This file is copied to spec/ when you run 'rails generate rspec:install' 2require 'spec_helper' 3ENV['RAILS_ENV'] ||= 'test' 4 5require File.expand_path('../config/environment', __dir__) 6 7# Prevent database truncation if the environment is production 8abort("The Rails environment is running in production mode!") if Rails.env.production? 9require 'rspec/rails' 10# Add additional requires below this line. Rails is not loaded until this point! 11 12# Requires supporting ruby files with custom matchers and macros, etc, in 13# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are 14# run as spec files by default. This means that files in spec/support that end 15# in _spec.rb will both be required and run as specs, causing the specs to be 16# run twice. It is recommended that you do not name files matching this glob to 17# end with _spec.rb. You can configure this pattern with the --pattern 18# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. 19# 20# The following line is provided for convenience purposes. It has the downside 21# of increasing the boot-up time by auto-requiring all files in the support 22# directory. Alternatively, in the individual `*_spec.rb` files, manually 23# require only the support files necessary. 24# 25Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f } 26 27# Checks for pending migrations and applies them before tests are run. 28# If you are not using ActiveRecord, you can remove these lines. 29begin 30 ActiveRecord::Migration.maintain_test_schema! 31rescue ActiveRecord::PendingMigrationError => e 32 puts e.to_s.strip 33 exit 1 34end 35RSpec.configure do |config| 36 # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 37 config.fixture_path = "#{::Rails.root}/spec/fixtures" 38 39 # If you're not using ActiveRecord, or you'd prefer not to run each of your 40 # examples within a transaction, remove the following line or assign false 41 # instead of true. 42 config.use_transactional_fixtures = true 43 44 # RSpec Rails can automatically mix in different behaviours to your tests 45 # based on their file location, for example enabling you to call `get` and 46 # `post` in specs under `spec/controllers`. 47 # 48 # You can disable this behaviour by removing the line below, and instead 49 # explicitly tag your specs with their type, e.g.: 50 # 51 # RSpec.describe UsersController, :type => :controller do 52 # # ... 53 # end 54 # 55 # The different available types are documented in the features, such as in 56 # https://relishapp.com/rspec/rspec-rails/docs 57 config.infer_spec_type_from_file_location! 58 59 # Filter lines from Rails gems in backtraces. 60 config.filter_rails_from_backtrace! 61 # arbitrary gems may also be filtered via: 62 # config.filter_gems_from_backtrace("gem name") 63 64 config.include LoginMacros 65 config.include FactoryBot::Syntax::Methods 66 67end

エラーから考えられる原因

File.expand_path('../config/environment', **dir**)は本質的な問題の副次作用らしく、 undefined local variable or method x' for main:Object がエラーの本質的なエラーなのかと思いました。

試したこと

・method 'x' を定義していないですがどこかでタイポしてしまったのかと思い探すも見つからず。

ruby

1NameError: 2 undefined local variable or method `x' for main:Object

・エラー対象のコード確認するも問題ないです

ruby

1require File.expand_path('../config/environment', __dir__)

・実装中のテストをコメントアウトするも結果変わらず

参考にしたURL

Failure/Error: require File.expand_path('/../config/environment', dir) というエラー - Qiita

config/schedule.rbのrequireメモ|ひびきき|note

RSpecでFailure/Error: require File.expand_path('../config/environment', dir)が出たときの対処法 - Qiita

よろしくお願いします。

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

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

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

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

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

guest

回答1

0

自己解決

解決しました!

エラーをしっかり読みます。。

タイポです。。

エラー文

# ./config/initializers/simple_form_bootstrap.rb:1:in `<top (required)>'

rails c をするも同じエラーに遭遇

config/initializers/simple_form_bootstrap.rb:1:in `<top (required)>': undefined local variable or method `x' for main:Object (NameError)

/config/initializers/simple_form_bootstrap.rb:1:in を確認したところ、 x が紛れ込んでいました。。

エラー文をしっかり読みます。。

投稿2021/09/16 06:42

mmmaaarrrkkk000

総合スコア16

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問