A lot has been written about engine development, and using a dummy app for testing.
In our case we're developing an engine that is not a stand-alone entity, but has dependencies on a real Rails 3 application. We still want this code to live in an engine, and not become part of the app, because the engine's job is to import data from a legacy system that has its own tables and model mappings, and we want to eventually remove it again.
The data mappings between the old legacy tables and the new schema are complex, and we want to TDD (using rspec) the engine.
/spec/dummy_app
with a git submodule pointing to a real Rails 3 app.config/application.rb
, to require the engine (which is what the dummy app does, as explained on pages 15-16 of the book).lib
folder into the load path $:
in spec_helper
and the paths are available.require
into spec_helper.rb
didn't solve the problem.Thoughts?
So, I figured out a few things and will answer my own question, now that I got it to work.
For simplicity, I'm going to refer to the name of my gem as "my_gem" and "MyGem":
In the engine.rb
file, I added:
require 'my_gem'
require 'rails'
This fixed errors of the type:
my_gem/lib/my_gem/engine.rb:2: uninitialized constant MyGem::Rails (NameError)
In spec_helper.rb
, I added right up top:
require 'bundler/setup'
require 'my_gem'
This is to make sure Bundler is initialized right away, and not through the app. That way I can load MyGem here, and it'll be hooked into the initialization sequence of the app. This fixes NameError
exceptions for the engine's model classes.
That leaves the question of what Gemfile
to use. The trouble is that my app has its own gemfile and the gem/engine needs its separate dependencies in its own Gemfile
.
I couldn't find any API for Bundler to pass two Gemfile
to it, in fact Bundler seems to be built around the assumption of a single authoritative Gemfile
. So I generate one in spec_helper
. I take the app's gemfile and append gemspec
which points to the gem's dependency in GemSpec format. (The hint about gemspec
is missing from Jose Valim's book by the way).
I don't know if there's a better way than concatenating files during test startup. If you know of one, please respond.
Helpful resources were:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With