How to develop a Rails3 engine against a real app, using RSpec?

让人想犯罪 __ 提交于 2019-12-05 13:09:24

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!