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

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

问题描述

因此,Rails 3.1带有一个鲜为人知的方便的rails g插件新生成器,它为您提供了一个适合rails gem插件的框架。 [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处引导。



我之所以这样描述,是因为我认为很多人不知道这个新的发生器,它很好地设置了它。

但是我的问题是,有没有人想过如何用rspec来做到这一点呢?我试图遵循相同的原则DIY来为rspec在rails插件gem中设置这样的东西,但我遇到了各种令人困惑的障碍,并且希望也许有人已经找到了它(或者有兴趣计算它为我们其他人,嘿)。

解决方案

创建不带测试单元的插件并指定虚拟应用程序的路径:

  rails插件新foobar --skip-test-unit --dummy-path = spec / dummy 

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

  Gem :: Specification.new do | s | 



s.add_development_dependencyrspec-rails
end

运行<$ c
$ b

从虚拟应用程序创建一个符号链接到plugin spec目录并运行Rspec安装生成器:

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

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

 需要File.expand_path(../../ config / environment,__FILE__)

为此:

 需要File.expand_path(../ dummy / config / environment,__FILE__)

现在您可以从插件的根目录运行Rspec将从虚拟应用程序中挑选规格。

  bundle exec rspec spec 

I wr详细了解这一点,展示如何在虚拟应用程序的rails插件中设置capybara,spork和guard:

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


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]

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.

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).

解决方案

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

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

Run bundle install

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 -

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__)

To this:

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

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

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插件宝石,虚拟测试应用程序,rspec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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