Rails5 Fails/Error When Running Rspec

Asked 2 years ago, Updated 2 years ago, 90 views

I am creating a task management application called taskleaf in accordance with the reference book rails5 Quick Learning Exercise Guide, but I have a failure/error error when running Rspec, so I would like to ask you a question.

In the root directory
Users/users/rails/taskleaf After running $bundle exec rspec spec/system/tasks_spec.rb, an error statement similar to the following appears:

Supplemental
The file containing ('../config/environment',__dir__) is spec/rails_helper.rb.

Thank you for your cooperation.

environment
Rails 5.2.4
Ruby 2.6.3

↓ The following is the full error statement.

An error occurred while loading./spec/system/tasks_spec.rb.
Failure/Error: require File.expand_path('../config/environment',__dir__)


NameError:
  uninitialized constant BetterErrors
# ./vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:in`block in load_missing_constant'
# ./vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in`without_bootsnap_cache'
# ./vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:in`resque in load_missing_constant'
# ./vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:58:in`load_missing_constant'
# ./config/initializers/better_errors.rb:1:in`<main>'
# ./vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
# ./vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/lib/rails/engine.rb:663:in `block in load_config_initializer'
# ./vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4/lib/active_support/notifications.rb:170:in `instrument'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/lib/rails/engine.rb:662:in `load_config_initializer'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/lib/rails/engine.rb: 620: in `block(2 levels) in<class:Engine>'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/lib/rails/engine.rb: 619: in `each'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/lib/rails/engine.rb: 619:in`block in<class:Engine>'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/lib/rails/initialized.rb:32:in`instance_exec'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/lib/rails/initialized.rb:32:in `run'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/lib/rails/initializeable.rb: 61: in `block in run_initializers'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/lib/rails/initialized.rb:50:in `each'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/lib/rails/initialized.rb:50:in`tsort_each_child'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/lib/rails/initializeable.rb:60:in `run_initializeers'
# ./vendor/bundle/ruby/2.6.0/gems/rails-5.2.4/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/tasks_spec.rb:1:in `require'
# ./spec/system/tasks_spec.rb:1:in`<top(required)>'
# ------------------
# --- Caused by :-
# NameError:
#   uninitialized constant BetterErrors
#   ./vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:60:in`block in load_missing_constant'
No examples found.


Finished in 0.00005 seconds (files book 4.78 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

tasks_spec.rb

require 'rails_helper'

descriptive 'task management', type: :system do
  let(:user_a) {FactoryBot.create(:user,name:'UserA',email:'[email protected]')}
  let(:user_b){FactoryBot.create(:user,name:'UserB',email:'[email protected]')}
  let!(:task_a) {FactoryBot.create(:task,name:'First Task',user:user_a)}

  before do
    visit login_path
    fill_in 'email address', with:login_user.email
    fill_in 'password', with: login_user.password
    click_button 'Log in'
  e n d

  shared_examples_for 'Displays tasks created by user A' do
    it {expect(page).to have_content 'First Task'}
  e n d

  describe 'list feature' do
    context 'when user A is logged in' do
      let(:login_user) {user_a}

      it_behaves_like 'A task created by user A is displayed'
    e n d

    context 'when user B is logged in' do
      let(:login_user) {user_b}

      it 'User A created task is not displayed' do
        expect(page).to have_no_content 'First Task'
      e n d
    e n d
  e n d

  describe 'Advanced View' do
    context 'when user A is logged in' do
      let(:login_user) {user_a}

      before do
        visit task_path(task_a)
      e n d

      it_behaves_like 'A task created by user A is displayed'
    e n d
  e n d

  describe 'New feature' do
    let(:login_user) {user_a}
    let(:task_name){'Write new test'}#Set as default

    before do
      visit new_task_path
      fill_in 'Name', with:task_name
      click_button 'Register'
    e n d

    context 'when you enter a name on the new screen' do
      it 'Registers Successfully' do
        expect(page).to have_selector '.alert-success', text: 'Write a new test'
      e n d
    e n d

    context 'when name is not entered on new screen' do
      let(:task_name){'}

      it's getting 'error' do
        within'#error_explanation'do
          expect(page).to have_content 'Please enter a name'
        e n d
      e n d
    e n d
  e n d
e n d

rails_helper.rb

#This file is copied to spec/when you run 'rails generate rspec:install'
require'spec_helper'
ENV['RAILS_ENV']||='test'

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

# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require'rspec/rails'
# Add additional requirements below this line.Rails is not loaded until this point!

# Requirements supporting ruby files with custom matches and macros, etc., in
# spec/support/and its subdirectories.Files matching `spec/**/*_spec.rb`are
# run as spec files by default.This means that files in spec / support that end
# in_spec.rb will both be required and run as specifics, causing the specific to be
# runtime.It is recommended that you do not name files matching this globe to
# end with_spec.rb.You can configure this pattern with the -- pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convention purpos. It is the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory.Alternatively, in the individual`*_spec.rb`files, manually
# require only the support files necessary.
#
# Dir [Rails.root.join('spec', 'support', '**', '*.rb')] .each {|f|require f}

# Checks for pending migrations and apps before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError=>e
  puts e.to_s.strip
  exit1
e n d
RSpec.configure do | config |
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixes
  config.fixure_path="#{:Rails.root}/spec/fixes"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples with a transaction, remove the following line or assign false
  # installed of true.
  config.use_transactional_fixes=true

  # RSpec Rails can automatically mix in different behaviors to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post`inspecs under`spec/controllers`.
  #
  # You can disable this behavior by removing the line below, and install
  # explicitly tag your specifications with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type=>:controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may be filtered via:
  # config.filter_gems_from_backtrace("gem name")
e n d

spec_helper.rb

#This file was generated by the `rails generate rspec:install`command.Conventionally, all
# specs live under a `spec` directory, which RSspec addresses to the `$LOAD_PATH`.
# The generated`.rspec`file contains`--require spec_helper`which will cause
# This file to always be loaded, without need to explicitly require it in any
# files.
#
# Given that is always loaded, you are encountered 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, consumer making
# separate helper file that require the additional dependencies and performances
# the additional setup, and require it from the spec files that actively need
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration

require 'capybara/rspec'

RSpec.configure do | config |
  config.before(:each, type::system)do
    drive_by —Selenium_chrome_headless
  e n d

  # 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 RSpec4. It makes the `description`
    # and `failure_message` of custom matches 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
  e n d

  # 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
    # real object.This is generally recommended, and will default to
    # `true` in RSpec4.
    mocks.verify_partial_doubles=true
  e n d

  # This option will default to `: apply_to_host_groups` in RSpec4(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
  # inherit by the metadata hash of host groups and examples, again 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 offers
  # aliases for `it`, `describe`, and `context` that include`: focus`
  # metadata: `fit', `fdescribe` and `fcontext', responsively.
  config.filter_run_when_matching:focus

  # Allows RSspec 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 common 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"
  e n d

  # 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 specifics in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by provisioning
  # The seed, which is printed after each run.
  #     --seed1234
  config.order= —Random

  # Seed global randomization in this process using the `--seed` CLI option.
  # Setting this allows you to use ` -- seed ` to determinely reproduce
  # test failures related to randomization by passing the same ` --seed `value
  # as the one that triggered the failure.
  Kernel.sland config.seed
=end
e n d

Gemfile

source 'https://rubygems.org'
git_source(:github){|repo|"https://github.com/#{repo}.git"}

ruby '2.6.3'

# Bundle edge Rails installed: gem 'rails', github: 'rails/rails'
gem 'rails', '~>5.2.3'
# Use postgresql as the database for Active Record
gem'sqlite3'
# Use Puma as the app server
gem 'puma', '~>3.11'
# Use SCSS for stylesheets
gem'sass-rails', '~>5.0'
# Use Uglifier as compressor for JavaScript assemblies
gem 'uglifer', '>=1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtime
# gem'mini_racer', platforms::ruby

# Use CoffeeScript for.coffee assemblies 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', '~>4.0'
# Use ActiveModel has_secure_password
gem 'bcrypt', '>=3.1.10'

# Use ActiveStorage variant
# gem'mini_magic', '~>4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group::development

# Reduce boot times through caching; required in config /boot.rb
gem 'bootsnap', '>=1.1.0', require: false

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', '~> 5.1', '>= 5.1.1'
e n d

group —Development do
  # Access an interactive console on exception pages or by calling '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's spring-watcher-listen', '~>2.0.0'

  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'pry-rails'

e n d

group —test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>=2.15'
  gem'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'webdrivers', '~>3.0'
  gem 'rspec-rails', '~>3.9'
  gem 'factory_bot_rails', '~> 5.1', '>= 5.1.1'
e n d

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem'tzinfo-data', platforms: [:mingw,:mswin,:x64_mingw,:jruby]

gem'slim-rails'
gem 'html2lim'

gem'bootstrap'
gem'nokogiri', '~>1.10', '>=1.10.5'
gem 'rails_autolink'

gem 'pry-byebug', group::development
gem 'pry-doc'

__dir__spec.rb

require_relative'../../spec_helper'

describe "Kernel #__dir__" do
  it "returns the real name of the directory containing the current-executing file" do
    __dir__.should==File.realpath(File.dirname(__FILE__))
  e n d

  context "when used in event with a given filename" do
    it "returns File.dirname(filename)"do
      even("__dir__", nil, "foo.rb").should=="."
      eval("__dir__", nil, "foo/bar.rb").should=="foo"
    e n d
  e n d

  context "when used in average with top level binding" do
    it "returns the real name of the directory containing the current-executing file" do
      eval("__dir__", binding).should==File.realpath(File.dirname(__FILE__))
    e n d
  e n d
e n d

ruby-on-rails rspec

2022-09-30 21:45

2 Answers

./config/initials/better_errors.rb
appears to be referring to the undefined BetterErrors.
BetterErrors, as described in "of" rel ""nofollow noforeferrer">BetterErrors, must be checked to see if defined in if defined?(BetterErors).


2022-09-30 21:45

better_errors attempting to load the initializer file of betterErrors class.Since this is a flow error, why don't you delete and run ./config/initializers/better_errors.rb?

By default, this file does not appear to be created.Was it added in the book?
https://github.com/BetterErrors/better_errors#set-maximum-variable-size-for-inspector

Also, would you like to check the definition in Gemfile?

This will take effect in the development, test, environment.

 group:development,:test do
  gem "better_errors"
e n d

This way, it can only be enabled in development, so it may not be loaded during rspec runtime (test environment).

 group:development
  gem "better_errors"
e n d


2022-09-30 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.