How to develop a Rails3 engine against a real application using RSpec?

Much has been written about developing the engine and using a dummy application for testing.

In our case, we are developing a engine that is not a standalone object, but has dependencies on a real Rails 3 application. We still want this code to work in the engine and not become part of the application, because the task on the engine is to import data from a legacy system that has its own tables and model comparisons, and we want to eventually delete it again.

The data mappings between the old old tables and the new schema are complex, and we want the TDD (using rspec) engine.

  • I followed in the book of Jose Valim “Creating Rail Applications” and use enginex gem .
  • I replaced /spec/dummy_app with a git submodule pointing to a real Rails 3 application.
  • I am having problems loading models from the engine (undefined errors), because the actual Gemfile application does not point to the engine, and I also can’t modify config/application.rb to require working with the engine (this is what the dummy application does, like explained on pages 15-16 of the book).
  • I include the engine lib folder in the $: boot path in spec_helper and the paths are available.
  • Putting require in spec_helper.rb did not solve the problem.
  • I am wondering if there is an internal Rails API (or a clever monkey patch) to connect to the boot sequence of a real application and require working with it, without having to change the real application code (as in the submodule).
  • Another problem that I'm not quite sure about is that I have 2 Gemfiles (one in the engine and one in the application), and when the engine is active, they should both be used.

Thoughts?

+4
source share
1 answer

So, I figured out a few things and will answer my own question, now that I got it to work.

For simplicity, I will refer to the name of my gem as "my_gem" and "MyGem":

In the engine.rb file engine.rb I added:

 require 'my_gem' require 'rails' 

These are fixed errors like:

 my_gem/lib/my_gem/engine.rb:2: uninitialized constant MyGem::Rails (NameError) 

In spec_helper.rb I added top to top:

 require 'bundler/setup' require 'my_gem' 

This means that the Bundler is initialized immediately, and not through the application. That way I can download MyGem here and it will be connected to the application initialization sequence. This fixes NameError exceptions for engine model classes.

This leaves the question of what to use Gemfile . The trouble is that my application has its own gemfile, while gem / engine needs its separate dependencies in its own Gemfile .

I could not find an API for the Bundler to pass two Gemfile , in fact the Bundler seems to be built around the assumption of one authoritative Gemfile . Therefore, I create one in spec_helper . I take the gemfile of the application and add gemspec , which points to the gem dependency in the GemSpec format. (By the way, in the book of Jose Valim there is no clue about gemspec ).

I do not know if there is a better way than concatenating files while running the test. If you know one, answer.

Useful resources were:

+8
source

All Articles