Question

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?

Was it helpful?

Solution 3

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

OTHER TIPS

One of the well known rails engines, rails_admin (https://github.com/sferik/rails_admin) uses that approach, so I believe it can be considered a good practice.

What's the load order for Rails app and Rails app's Rails Engines?

My guess is that the Rails app Gemfile is the determining factor for a gem being loaded or not loaded. This might be worth a try in a Rails test app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top