1

I am trying to write rspec tests for a rails 6 project.

When I run rspec spec/controllers/user_spec.rb I get this error

    Migrations are pending. To resolve this issue, 
      run:rails db:migrate RAILS_ENV=test 
    No examples found.

So I run rails db:migrate RAILS_ENV=test and then I get this error


    rails aborted!
    LoadError: cannot load such file -- rails_helper
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:89:in `register'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:40:in `rescue in require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:26:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/zeitwerk-2.2.1/lib/zeitwerk/kernel.rb:23:in `require'
    /Users/MacBookAir/workspace/squealer/spec/factories/devise.rb:1:in `<main>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/find_definitions.rb:20:in `block (2 levels) in find_definitions'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/find_definitions.rb:19:in `each'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/find_definitions.rb:19:in `block in find_definitions'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/find_definitions.rb:15:in `each'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/find_definitions.rb:15:in `find_definitions'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/reload.rb:6:in `reload'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot_rails-5.1.1/lib/factory_bot_rails/railtie.rb:26:in `block in <class:Railtie>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:62:in `with_execution_control'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:67:in `execute_hook'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:52:in `block in run_load_hooks'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:51:in `each'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:51:in `run_load_hooks'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/application/finisher.rb:129:in `block in <module:Finisher>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/initializable.rb:32:in `instance_exec'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/initializable.rb:32:in `run'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/initializable.rb:61:in `block in run_initializers'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/initializable.rb:60:in `run_initializers'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/application.rb:363:in `initialize!'
    /Users/MacBookAir/workspace/squealer/config/environment.rb:14:in `<main>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/zeitwerk-2.2.1/lib/zeitwerk/kernel.rb:23:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/dependencies.rb:325:in `block in require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/dependencies.rb:291:in `load_dependency'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/dependencies.rb:325:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/application.rb:339:in `require_environment!'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/application.rb:515:in `block in run_tasks_blocks'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/command.rb:48:in `invoke'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/commands.rb:18:in `<main>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/dependencies.rb:325:in `block in require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/dependencies.rb:291:in `load_dependency'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/dependencies.rb:325:in `require'
    /Users/MacBookAir/workspace/squealer/bin/rails:9:in `<top (required)>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
    /Users/MacBookAir/workspace/squealer/bin/spring:15:in `<top (required)>'
    bin/rails:3:in `load'
    bin/rails:3:in `<main>'

    Caused by:
    Bootsnap::LoadPathCache::FallbackScan:

    Tasks: TOP => db:migrate => db:load_config => environment
    (See full trace by running task with --trace)

So then I remove the bootsnap gem and go to config/boot.rb and remove this line require 'bootsnap/setup'

Then I run bundle install and then try to run rails db:migrate RAILS_ENV=test once more.

I get another error


    rails aborted!
    LoadError: cannot load such file -- rails_helper
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/zeitwerk-2.2.1/lib/zeitwerk/kernel.rb:23:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/zeitwerk-2.2.1/lib/zeitwerk/kernel.rb:23:in `require'
    /Users/MacBookAir/workspace/squealer/spec/factories/devise.rb:1:in `<top (required)>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/find_definitions.rb:20:in `load'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/find_definitions.rb:20:in `block (2 levels) in find_definitions'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/find_definitions.rb:19:in `each'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/find_definitions.rb:19:in `block in find_definitions'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/find_definitions.rb:15:in `each'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/find_definitions.rb:15:in `find_definitions'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot-5.1.1/lib/factory_bot/reload.rb:6:in `reload'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/factory_bot_rails-5.1.1/lib/factory_bot_rails/railtie.rb:26:in `block in <class:Railtie>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:62:in `with_execution_control'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:67:in `execute_hook'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:52:in `block in run_load_hooks'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:51:in `each'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/lazy_load_hooks.rb:51:in `run_load_hooks'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/application/finisher.rb:129:in `block in <module:Finisher>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/initializable.rb:32:in `instance_exec'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/initializable.rb:32:in `run'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/initializable.rb:61:in `block in run_initializers'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/initializable.rb:60:in `run_initializers'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/application.rb:363:in `initialize!'
    /Users/MacBookAir/workspace/squealer/config/environment.rb:14:in `<top (required)>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/zeitwerk-2.2.1/lib/zeitwerk/kernel.rb:23:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/zeitwerk-2.2.1/lib/zeitwerk/kernel.rb:23:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/dependencies.rb:325:in `block in require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/dependencies.rb:291:in `load_dependency'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/activesupport-6.0.1/lib/active_support/dependencies.rb:325:in `require'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/application.rb:339:in `require_environment!'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/application.rb:515:in `block in run_tasks_blocks'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/command.rb:48:in `invoke'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/railties-6.0.1/lib/rails/commands.rb:18:in `<top (required)>'
    /Users/MacBookAir/workspace/squealer/bin/rails:9:in `require'
    /Users/MacBookAir/workspace/squealer/bin/rails:9:in `<top (required)>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
    /Users/MacBookAir/.rvm/gems/ruby-2.6.3/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
    /Users/MacBookAir/workspace/squealer/bin/spring:15:in `<top (required)>'
    bin/rails:3:in `load'
    bin/rails:3:in `<main>'
    Tasks: TOP => db:migrate => db:load_config => environment
    (See full trace by running task with --trace)

At this point, I am ready to toss my laptop into the trash. I don't even know what the zeitwerk gem is or what a thread safe code loader does. I just know it's required for Rails 6.

My github repo is here

Does anyone know what I am doing wrong?

David Lee
  • 571
  • 6
  • 20
  • Have you tried the suggestions on here: https://stackoverflow.com/questions/25800122/error-when-trying-to-run-rspec-require-cannot-load-such-file-rails-helper – The Wizard Nov 21 '19 at 04:32
  • Yes, I have seen that post. I'm on rspec 3.9 and I've done ```rails generate rspec:install``` which is how I got the rails_helper.rb file. It still didn't help me though – David Lee Nov 21 '19 at 18:57

2 Answers2

1

Ah - I figured it out. I didn't read my error logs carefully.

I am using the Devise gem. In my error logs, the 8th line down mentioned a file in spec/factories/devise.rb

I looked at that file and saw that I had placed require 'rails_helper.rb' at the top of the file. I removed that line and now I can run rails db:migrate RAILS_ENV=TEST

Guess this is a lesson in reading the error logs carefully

dopatraman
  • 13,416
  • 29
  • 90
  • 154
David Lee
  • 571
  • 6
  • 20
0

I just went through your repo. You actually don't have a rails_helper file. You should do a find and replace for rails_helper, replacing it with test_helper

Adim
  • 1,716
  • 12
  • 13
  • 2
    So I actually do have the rails_helper.rb file. It's in spec/rails_helper.rb It gets created automatically when you run ```rails generate rspec:install``` I believe that test_helper.rb is only needed for Minitest, which I don't use. – David Lee Nov 21 '19 at 18:58