如何使用 RSpec 针对真实应用开发 Rails3 引擎? [英] How to develop a Rails3 engine against a real app, using RSpec?

查看:33
本文介绍了如何使用 RSpec 针对真实应用开发 Rails3 引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于引擎开发和使用虚拟应用进行测试的文章很多.

A lot has been written about engine development, and using a dummy app for testing.

在我们的例子中,我们正在开发一个引擎,它不是一个独立的实体,而是依赖于一个真正的 Rails 3 应用程序.我们仍然希望这些代码存在于引擎中,而不是成为应用程序的一部分,因为引擎的工作是从具有自己的表和模型映射的遗留系统中导入数据,我们希望最终再次将其删除.

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.

旧的遗留表和新模式之间的数据映射很复杂,我们希望 TDD(使用 rspec)引擎.

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

  • 我关注了 Jose Valim 的书Crafting Rails Appliations",并且正在使用 Jose Valim 的书Crafting Rails Appliations"一个 href="https://github.com/josevalim/enginex" rel="nofollow">enginex gem.
  • 我已将 /spec/dummy_app 替换为指向真正 Rails 3 应用的 git 子模块.
  • 我无法从引擎加载模型(未定义符号错误),因为真实应用的 Gemfile 未指向引擎,而且我也无法修改 config/application.rb,需要引擎(这是虚拟应用程序所做的,如本书第 15-16 页所述).
  • 我将引擎的 lib 文件夹包含在 spec_helper 中的加载路径 $: 中,并且路径可用.
  • require 放入 spec_helper.rb 并没有解决问题.
  • 我想知道是否有一个内部 Rails API(或一个聪明的猴子补丁)来挂钩真实应用程序的启动序列并需要引擎,而无需修改真实应用程序的代码(因为它在子模块中)).
  • 另一个我不太确定的问题是我有 2 个 Gemfile(一个在引擎中,一个在应用程序中),当引擎处于活动状态时,应该同时使用它们.
  • I've followed Jose Valim's book "Crafting Rails Appliations" and am using the enginex gem.
  • I've replaced /spec/dummy_app with a git submodule pointing to a real Rails 3 app.
  • I'm having trouble loading the models from the engine (undefined symbol errors), as the real app's Gemfile doesn't point to the engine, and I also can't modify config/application.rb, to require the engine (which is what the dummy app does, as explained on pages 15-16 of the book).
  • I'm including the engine's lib folder into the load path $: in spec_helper and the paths are available.
  • Putting the require into spec_helper.rb didn't solve the problem.
  • I'm wondering if there is an internal Rails API (or a clever monkey patch) to hook into the boot sequence of the real app and require the engine, without having to modify the real app's code (as it's in a submodule).
  • Another issue I'm not fully sure about is that I have 2 Gemfiles (one in the engine and one in the app), and when the engine is active, they should both be used.

想法?

推荐答案

所以,我想出了一些事情,并将回答我自己的问题,现在我开始工作了.

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

为简单起见,我将我的 gem 名称称为my_gem"和MyGem":

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

engine.rb 文件中,我添加了:

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)

spec_helper.rb 中,我在顶部添加:

In spec_helper.rb, I added right up top:

require 'bundler/setup'
require 'my_gem'

这是为了确保 Bundler 立即初始化,而不是通过应用程序初始化.这样我就可以在这里加载 MyGem,它会被挂接到应用程序的初始化序列中.这修复了引擎模型类的 NameError 异常.

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.

这就留下了使用什么 Gemfile 的问题.问题是我的应用程序有自己的 gemfile,gem/引擎需要在自己的 Gemfile 中单独依赖.

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.

我找不到任何可以让 Bundler 将两个 Gemfile 传递给它的 API,事实上 Bundler 似乎是围绕单一权威 Gemfile 的假设构建的.所以我在 spec_helper 中生成了一个.我获取应用程序的 gemfile 并附加 gemspec,它以 GemSpec 格式指向 gem 的依赖项.(顺便提一下,Jose Valim 的书中没有关于 gemspec 的提示).

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.

有用的资源是:

  • Clarifying the Roles of the .gemspec and Gemfile by Yehuda Katz
  • Bundler Documentation for gemspec

这篇关于如何使用 RSpec 针对真实应用开发 Rails3 引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆