1

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
ReggieB
  • 8,100
  • 3
  • 38
  • 46

3 Answers3

0

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.

cthulhu
  • 3,749
  • 1
  • 21
  • 25
  • That's an interesting example. However, I notice that they also define gems to only be used in development. For example pry in: https://github.com/sferik/rails_admin/blob/bba8cba7e7e9391f653057ac12e119f532ffd6ba/Gemfile. Is this bad practice? If so would that make this example a bad "good practice" example? – ReggieB Aug 13 '13 at 09:34
0

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.

westonplatter
  • 1,475
  • 2
  • 19
  • 30
  • The code example I've added works. I am not asking what works, but rather what is best practice. – ReggieB Aug 13 '13 at 10:21
  • Sorry, looking at this again, I realise your answer is relevant. The load order determines whether the gems would be loaded into the host app's test environment. Good point! – ReggieB Aug 13 '13 at 10:34
0

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

ReggieB
  • 8,100
  • 3
  • 38
  • 46
  • @MattW. Yours is the first criticism I have received for this answer. My pull request was not laughed out of github. The only discussion was around the wording - not the strategy. If you could suggest a better way of ensuring a gem needed when testing an engine, only gets loaded in that environment then I would welcome it. If you want a test case, add web_mock to an engine via `add_development_dependency` and then try calling an external service from the development environment. – ReggieB May 28 '16 at 16:18