简体   繁体   English

LoadError: cannot load such file — rails_helper (Rails 6 and Rspec)

[英]LoadError: cannot load such file — rails_helper (Rails 6 and Rspec)

I am trying to write rspec tests for a rails 6 project.我正在尝试为 rails 6 项目编写 rspec 测试。

When I run rspec spec/controllers/user_spec.rb I get this error当我运行rspec spec/controllers/user_spec.rb我得到这个错误

    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 db:migrate RAILS_ENV=test然后我得到这个错误


    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'然后我将 bootsnap gem 和 go 删除到 config/boot.rb 并删除此行require 'bootsnap/setup'

Then I run bundle install and then try to run rails db:migrate RAILS_ENV=test once more.然后我运行 bundle install 然后再次尝试运行rails db:migrate RAILS_ENV=test

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.我什至不知道 zeitwerk gem 是什么,也不知道线程安全代码加载器是做什么的。 I just know it's required for Rails 6.我只知道 Rails 6 需要它。

My github repo is here我的 github 仓库在这里

Does anyone know what I am doing wrong?有谁知道我做错了什么?

I just went through your repo.我刚刚浏览了你的回购。 You actually don't have a rails_helper file.您实际上没有rails_helper文件。 You should do a find and replace for rails_helper , replacing it with test_helper您应该查找并替换rails_helper ,将其替换为test_helper

Ah - I figured it out.啊——我想通了。 I didn't read my error logs carefully.我没有仔细阅读我的错误日志。

I am using the Devise gem.我正在使用 Devise gem。 In my error logs, the 8th line down mentioned a file in spec/factories/devise.rb在我的错误日志中,第 8 行下面提到了 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.我查看了那个文件,发现我在文件顶部放置了require 'rails_helper.rb' I removed that line and now I can run rails db:migrate RAILS_ENV=TEST我删除了那行,现在我可以运行rails db:migrate RAILS_ENV=TEST

Guess this is a lesson in reading the error logs carefully猜猜这是仔细阅读错误日志的一个教训

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM