cannot load gems in test environment

2019-07-15 11:00发布

问题:

  • My app works.
  • I was able to run tests before, and now for some reason I can't.
  • What happened between now and then is that I vendorized activesupport (but I have since undone those changes, and in fact I am in a totally separate/unrelated branch).
  • Two commands I ran during my work on activesupport that I am unfamiliar with are bundle --deployment and bundle --no-deployment. Again, I believe I undid everything related to those changes (at least as far as git status is concerned - I always run git status, and am aware of all codebase changes).

I am aware that other people have had similar, but not this exact problem: https://stackoverflow.com/search?q=require+cannot+load+such+file+LoadError

I was playing around with vendorizing activesupport following this: How to vendor a modified version of active_support such that it is used in my Rails app?

And I had trouble when I tried pushing the vendorized activesupport branch to staging: Where is 'elsewhere' in "run `bundle install` elsewhere"?

Anyway, the point is that my app works (i.e. all the gems are obviously installed, Gemfile and Gemfile.lock are intact), yet I cannot run tests:

/Users/bsimpson/.rvm/gems/ruby-2.0.0-p481@books/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `require': cannot load such file -- minitest/rails (LoadError)
  from /Users/bsimpson/.rvm/gems/ruby-2.0.0-p481@books/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `block in require'
  from /Users/bsimpson/.rvm/gems/ruby-2.0.0-p481@books/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:214:in `load_dependency'
  from /Users/bsimpson/.rvm/gems/ruby-2.0.0-p481@books/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `require'
  from /Volumes/MyHD/Users/bsimpson/Dev/books/books/test/test_helper.rb:10:in `<top (required)>'
  from test/controllers/photos_controller_test.rb:1:in `require'
  from test/controllers/photos_controller_test.rb:1:in `<main>'

If I comment out line 10 of test/test_helper.rb, it just complains about the next require line.

dependencies.rb:229:in `require': cannot load such file -- minitest/rails/capybara (LoadError)
...
test/test_helper.rb:14:in `<top (required)>'

And when I comment out line 14, it complains about the next require line, and so on...

So it seems that in the test environment, the gems aren't seen. Thoughts?

.bundle/config

---
BUNDLE_WITHOUT: development:test

回答1:

The configuration BUNDLE_WITHOUT: development:test tells Bundler not to load gems from the development and test groups. Since this is your only configuration setting, you can safely delete the .bundle/config file and things should go back to working for you.

Alternatively, you could leave the file and only delete that line.

Is it possible that when you ran bundle --deployment, you actually ran bundle --deployment --without development test? These options are commonly used together, and options passed to bundle install (or bundle without a sub-command, which defaults to install) are remembered in the bundle/.config file. These options are "sticky" until they are explicitly removed, so if you use the --without flag once, you'll need to edit the file to get those groups back.

Also note that .bundle/config is usually excluded from git, so checking git status won't tell you if it changed. These settings are often machine-dependent, so it's a good idea to keep it excluded from git.