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

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

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

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

RSpec

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

Q&A

解決済

1回答

1933閲覧

【RSpec】RSpec実行時にエラー uninitialized constant Shoulda

Hamadust

総合スコア5

Ruby on Rails 5

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

RSpec

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

0グッド

0クリップ

投稿2021/07/24 09:01

前提・実現したいこと

RSpec実行時にエラーが起きてしまい、原因が分かりません。RSpecが正しく実行されるようにしたいです。

RSpec教本  「Everyday Rails - RSpecによるRailsテスト入門」 でRSpecの学習をしている初学者です。

テキストに沿って学習を進めていたはずなのですが、RSpec実行の際に

An error occurred while loading ./スペック名                      ~中略~                                       NameError:uninitialized constant Shoulda

というエラーが出るようになってしまいました。

途中までは以下のようなGem ・ Shoulda Matchersを使うようなテストが通っていました。

ruby

1it { is_expected.to validate_presence_of :email }

ケアレスミスなのかもしれませんが、設定ファイルなどを変更した心当たりもありません。
よろしくお願い致します。

環境

Ruby 2.5.0
Rails 5.1.7
rspec-rails 3.8.3

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

terminal

1$ bin/rspec 2 3Running via Spring preloader in process 5952 4 5An error occurred while loading ./spec/controllers/home_controller_spec.rb. 6Failure/Error: 7 Shoulda::Matchers.configure do |config| 8 config.integrate do |with| 9 with.test_framework :rspec 10 with.library :rails 11 end 12 end 13 14NameError: 15 uninitialized constant Shoulda 16# ./spec/rails_helper.rb:64:in `<top (required)>' 17# ./spec/controllers/home_controller_spec.rb:1:in `<top (required)>' 18# -e:1:in `<main>' 19 20An error occurred while loading ./spec/controllers/notes_controller_spec.rb. 21Failure/Error: 22 Shoulda::Matchers.configure do |config| 23 config.integrate do |with| 24 with.test_framework :rspec 25 with.library :rails 26 end 27 end 28 29NameError: 30 uninitialized constant Shoulda 31# ./spec/rails_helper.rb:64:in `<top (required)>' 32# ./spec/controllers/notes_controller_spec.rb:1:in `<top (required)>' 33# -e:1:in `<main>' 34 35[中略] 36 37Finished in 0.00037 seconds (files took 0.39113 seconds to load) 380 examples, 0 failures, 1 error occurred outside of examples

該当のソースコード

Gemfile

1source 'https://rubygems.org' 2 3git_source(:github) do |repo_name| 4 repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") 5 "https://github.com/#{repo_name}.git" 6end 7 8gem 'rails', '~> 5.1.1' 9gem 'sqlite3' 10gem 'puma', '~> 3.7' 11gem 'sass-rails', '~> 5.0' 12gem 'uglifier', '>= 1.3.0' 13gem 'coffee-rails', '~> 4.2' 14gem 'turbolinks', '~> 5' 15gem 'jbuilder', '~> 2.5' 16 17group :development, :test do 18 gem 'rspec-rails', '~> 3.6.0' 19 gem "factory_bot_rails", "~> 4.10.0" 20 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 21 gem 'capybara', '~> 3.25.0' 22 gem 'webdrivers' 23 gem 'launchy', '~> 2.4.3' 24 gem 'selenium-webdriver' 25 gem 'shoulda-matchers', 26 git: 'https://github.com/thoughtbot/shoulda-matchers.git', 27 branch: 'rails-5' 28end 29 30group :development do 31 gem 'web-console', '>= 3.3.0' 32 gem 'listen', '>= 3.0.5', '< 3.2' 33 gem 'spring' 34 gem 'spring-watcher-listen', '~> 2.0.0' 35 gem 'faker', require: false # for sample data in development 36 gem 'spring-commands-rspec' 37end 38 39gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 40 41gem 'bootstrap-sass' 42gem 'jquery-rails' 43gem 'devise' 44gem 'paperclip' 45gem 'geocoder' 46

spec/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' 4require File.expand_path('../../config/environment', __FILE__) 5# Prevent database truncation if the environment is production 6abort("The Rails environment is running in production mode!") if Rails.env.production? 7require 'rspec/rails' 8# Add additional requires below this line. Rails is not loaded until this point! 9require 'capybara/rspec' 10 11# Requires supporting ruby files with custom matchers and macros, etc, in 12# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are 13# run as spec files by default. This means that files in spec/support that end 14# in _spec.rb will both be required and run as specs, causing the specs to be 15# run twice. It is recommended that you do not name files matching this glob to 16# end with _spec.rb. You can configure this pattern with the --pattern 17# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. 18# 19# The following line is provided for convenience purposes. It has the downside 20# of increasing the boot-up time by auto-requiring all files in the support 21# directory. Alternatively, in the individual `*_spec.rb` files, manually 22# require only the support files necessary. 23# 24Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } 25 26# Checks for pending migration and applies them before tests are run. 27# If you are not using ActiveRecord, you can remove this line. 28ActiveRecord::Migration.maintain_test_schema! 29 30RSpec.configure do |config| 31 # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 32 config.fixture_path = "#{::Rails.root}/spec/fixtures" 33 34 # If you're not using ActiveRecord, or you'd prefer not to run each of your 35 # examples within a transaction, remove the following line or assign false 36 # instead of true. 37 config.use_transactional_fixtures = true 38 39 # RSpec Rails can automatically mix in different behaviours to your tests 40 # based on their file location, for example enabling you to call `get` and 41 # `post` in specs under `spec/controllers`. 42 # 43 # You can disable this behaviour by removing the line below, and instead 44 # explicitly tag your specs with their type, e.g.: 45 # 46 # RSpec.describe UsersController, :type => :controller do 47 # # ... 48 # end 49 # 50 # The different available types are documented in the features, such as in 51 # https://relishapp.com/rspec/rspec-rails/docs 52 config.infer_spec_type_from_file_location! 53 54 # Filter lines from Rails gems in backtraces. 55 config.filter_rails_from_backtrace! 56 # arbitrary gems may also be filtered via: 57 # config.filter_gems_from_backtrace("gem name") 58 # コントローラスペックで、Deviseのテストヘルパーを使用する 59 config.include Devise::Test::ControllerHelpers, type: :controller 60 config.include RequestSpecHelper, type: :request 61 config.include Devise::Test::IntegrationHelpers, type: :feature 62end 63 64Shoulda::Matchers.configure do |config| 65 config.integrate do |with| 66 with.test_framework :rspec 67 with.library :rails 68 end 69end 70

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

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

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

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

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

guest

回答1

0

自己解決

自己解決しました。

最後まで原因が分かりませんでしたが、Githubにアップされている大元のソースコードをrails_heleprにコピペした所、テストが通りました。

この作業自体は質問した当日にも行ったのですが…。

投稿2021/07/26 05:38

Hamadust

総合スコア5

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問