Rails 3.1 插件 gem、虚拟测试应用程序、rspec [英] Rails 3.1 plugin gem, dummy test app, rspec

查看:17
本文介绍了Rails 3.1 插件 gem、虚拟测试应用程序、rspec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以 Rails 3.1 附带了一个鲜为人知的方便的rails g plugin new"生成器,它为您提供了一个适合 rails gem 插件的框架.[http://guides.rubyonrails.org/plugins.html#or-generate-a-gemified-plugin]

So Rails 3.1 comes with a little-known handy "rails g plugin new" generator, which gives you a skeleton suitable for a rails gem plugin. [http://guides.rubyonrails.org/plugins.html#or-generate-a-gemified-plugin]

这样做的有用之处之一是方便地设置一些东西以使用 Test::Unit 进行测试.它为您提供了一个基本的虚拟 Rails 应用程序,您的测试可以在其上下文中运行,以测试仅在 Rails 应用程序副本中运行的引擎"行为.(它把它放在 ./test/dummy 中).但是您的测试仍在 my_gem/test 中,测试不在虚拟应用程序中.而 my_gem/test/test_helper.rb 就在那里,这样编写测试将在虚拟应用程序的上下文中运行,在 ../dummy/config/environment 启动.

One of the useful things this does, is set things up conveniently for testing with Test::Unit. It gives you a basic dummy Rails app that your tests can run in the context of, to test 'engine' behavior that only functions in the copy of a Rails app. (it puts it in ./test/dummy). But your tests are still in my_gem/test , the tests dont' live in the dummy app. And my_gem/test/test_helper.rb is there, written such that tests will be run in the context of the dummy app, booted over at ../dummy/config/environment.

我描述这个是因为我认为很多人不知道这个新的生成器,它设置得非常好.

I describe this because I think a lot of people don't know about this new generator, which sets things up so nicely.

但我的问题是,有没有人想出如何用 rspec 来做到这一点?我试图遵循相同的 DIY 原则,在 Rails 插件 gem 中为 rspec 设置这样的东西,但我遇到了各种令人困惑的障碍,我希望也许其他人已经弄清楚了(或者有兴趣弄清楚它)对于我们其他人来说,呵呵).

But my question is, has anyone figured out how to do this with rspec instead? I have tried to follow the same principles DIY to set things up like this for rspec in a rails plugin gem, but am running into various confusing roadblocks, and am hoping maybe someone else has already figured it out (or would be interested in figuring it out for the rest of us, heh).

推荐答案

创建没有 test-unit 的插件并指定虚拟应用程序的路径:

Create the plugin without test-unit and specify the path for the dummy application:

rails plugin new foobar --skip-test-unit --dummy-path=spec/dummy

将 rspec-rails 作为开发依赖添加到 gemspec 文件(foobar.gemspec):

Add rspec-rails as a development dependency to the gemspec file (foobar.gemspec):

Gem::Specification.new do |s|
  .
  .
  .
  s.add_development_dependency "rspec-rails"
end

运行捆绑安装

创建从虚拟应用程序到插件规范目录的符号链接并运行 Rspec 安装生成器:

Create a symlink from the dummy app to the plugin spec directory and run the Rspec install generator:

cd spec/dummy
ln -s ../../spec
rails generate rspec:install
cd -

现在编辑 spec/spec_helper.rb(或 Rails 4+ 中的 spec/rails_helper.rb,不确定旧版本)更改此行(第 3 行):

Now edit spec/spec_helper.rb (or spec/rails_helper.rb in rails 4+, not sure about older versions) changing this line (line 3):

require File.expand_path("../../config/environment", __FILE__)

为此:

require File.expand_path("../dummy/config/environment", __FILE__)

现在您可以从插件的根目录运行 Rspec,它也会从虚拟应用程序中获取规范.

Now you can run Rspec from the root of your plugin and it will pick up specs from the dummy application as well.

bundle exec rspec spec

我更详细地写了这篇文章,展示了如何在带有虚拟应用程序的 rails 插件中设置 capybara、spork 和 guard:

I wrote about this in more detail, showing how to also set up capybara, spork and guard in a rails plugin with a dummy application:

https://web.archive.org/web/20130125091106/http://namick.tumblr.com/post/17663752365/how-to-create-a-gemified-plugin-with-rails-3-2-rspec

这篇关于Rails 3.1 插件 gem、虚拟测试应用程序、rspec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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