A recent discussion here has brought konacha, an engine for testing
JavaScript code, to my attention. In the meantime, I have extracted
common code from several applications into an engine and there I would
like to add some tests for the scripts.
With current versions of Rails, when I create an engine with
$ rails new plugin my_engine --mountable
among all the other niceties I get is an dummy application for testing
purposes in test/dummy. Unfortunately, things are not set-up properly
for testing. Dependencies specified in my_engine.gemspec are not loaded,
or if they are they are loaded too late. As a consequence, the asset
paths from the jquery-rails engine are not added to the dummy
application's asset paths.
In order to get those engines loaded at the right time, I need to add
explicit requires in test/dummy/config/application.rb, just below the
generated require "my_engine". Ideally, none of these requires would be
necessary and Bundler.require (that's already there) would automatically
load all dependencies from the enclosing engine's gemspec.
For the specific case of konacha that still would not be enough (on top
of this, it needs to set different additional asset paths, then when in
an app environment), but it would be a good start.
Michael
A recent discussion here has brought konacha, an engine for testing
JavaScript code, to my attention. In the meantime, I have extracted
common code from several applications into an engine and there I would
like to add some tests for the scripts.
With current versions of Rails, when I create an engine with
$ rails new plugin my_engine --mountable
among all the other niceties I get is an dummy application for testing
purposes in test/dummy. Unfortunately, things are not set-up properly
for testing. Dependencies specified in my_engine.gemspec are not loaded,
or if they are they are loaded too late. As a consequence, the asset
paths from the jquery-rails engine are not added to the dummy
application’s asset paths.
In order to get those engines loaded at the right time, I need to add
explicit requires in test/dummy/config/application.rb, just below the
generated require “my_engine”. Ideally, none of these requires would be
necessary and Bundler.require (that’s already there) would automatically
load all dependencies from the enclosing engine’s gemspec.
I believe you can also add the dependancies to the Gemfile of the engine. In fact, I think jquery-rails is specified in the Gemfile but commented out.
I’m not saying this is ideal or anything, but I think that’s the “accepted” way to get it working.
Jeremy
Yes, I can do that and now I did. What I didn't expect is that gems
specifified in the Gemfile are treated differently than those specified
in the gemspec. Gems from the gemspec are not automatically required,
while those from the Gemfile are (unless the require: false option is
used).
As far as Rails concerned, case closed. Thanks!
Michael
What I usually do to work around such issues is to explicitly
require the required gems in my Engine main file:
https://github.com/rosenfeld/oojs/blob/master/lib/oojs.rb
As you can see, just setting the gem dependency in the gemspec is
not enough for automatically requiring them:
https://github.com/rosenfeld/oojs/blob/master/oojs.gemspec
That's another way, but you wouldn't want to use it for development
dependencies. Say you're using capybara-wekbit for testing your engine,
you still don't want to have it as a runtime dependency.
Michael
Sure, that doesn't apply for development dependencies.