Gem development: When your specs don't see dependencies from your Gemfile

Updated . Posted . Visible to the public.

When you develop a gem and you have a Gemfile in your project directory, you might be surprised that your gem dependencies aren't already required in your specs. Here is some info that should help you out:

  • Bundler actually doesn't automatically require anything. You need to call Bundler.require(:default, :your_custom_group1, ...) for that. The reason why you never had to write this line is that Rails does this for you when it boots the environment.
  • That also means that if you have an embedded Rails app in your spec folder (like has_defaults Show archive.org snapshot ), and you boot its environment, it should call Bundler.require for you and you don't need to require anything yourself.
  • If your embedded app is an old Rails 2.3 app, make sure it has been patched to call Bundler when booting Show archive.org snapshot . Also make sure the ENV["BUNDLE_GEMFILE"] in preinitializer.rb points to the correct file if you're organizing your Gemfiles in funny ways.
  • Finally, note that even with Bundler.require, Bundler will only require gems that are directly quoted in your Gemfile. It will not require gems that are dependencies of these gems. It's the job of a gem to require its dependencies. E.g. if you're working on consul which depends on memoizer, consul needs to say require 'memoizer' in lib/consul.rb or similiar.
Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2012-08-02 08:26)