Search code examples
ruby-on-railstestingrails-enginesgemfilegemspecs

Rails Engines: Where to define gems only used in testing


I'm building an engine, and I want to use VCR and Webmock for testing.

The documentation within the Gemfile generated when an engine is created, seems to suggest that all an engine's gems should be loaded via gemspec, but the only options for this are add_dependency and add_development_dependency. If I use the latter, VCR and Webmock get loaded into my development environment, and I then have to explicitly disable Webmock in the development environment. I'd rather not do that as a host app may want these gems to work in development, and my engine disabling them may be unexpected.

The obvious solution would appear to be to use the engine's Gemfile:

group :test do
  gem 'vcr'
  gem 'webmock'
end

Is this the right way to load gems that are only used when testing an engine?

Are there any gotchas doing this?


Solution

  • I believe the answer is that there is nothing wrong with declaring in an engine's Gemfile, gems only used for testing and debugging the engine code. Further, I think the Gemfile template should be made less ambiguous, and have submitted a pull request to this effect:

    https://github.com/rails/rails/pull/11881