[英]Rails 3.1 plugin gem, dummy test app, 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. 所以Rails 3.1附带了一个鲜为人知的“rails g plugin new”生成器,它为你提供了一个适合rails gem插件的骨架。 [http://guides.rubyonrails.org/plugins.html#or-generate-a-gemified-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. 这样做的一个有用的事情是为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.
它为您提供了一个基本的虚拟Rails应用程序,您的测试可以在上下文中运行,以测试仅在Rails应用程序副本中运行的“引擎”行为。 (it puts it in ./test/dummy).
(它把它放在./test/dummy中)。 But your tests are still in my_gem/test , the tests dont' live in the dummy app.
但是你的测试仍然在my_gem / test中,测试不会存在于虚拟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.
并且my_gem / test / test_helper.rb在那里编写,测试将在虚拟应用程序的上下文中运行,在../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? 但我的问题是,有没有人想出如何用rspec做这个呢? 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).
我曾尝试按照相同的原则DIY为rails specing中的rspec设置这样的东西,但我遇到了各种令人困惑的障碍,我希望也许别人已经弄明白了(或者有兴趣搞定它对我们其他人来说,嘿)。
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
): 将rspec-rails添加为gemspec文件(
foobar.gemspec
)的开发依赖foobar.gemspec
:
Gem::Specification.new do |s|
.
.
.
s.add_development_dependency "rspec-rails"
end
Run bundle install
运行
bundle install
Create a symlink from the dummy app to the plugin spec directory and run the Rspec install generator: 创建从虚拟应用程序到插件规范目录的符号链接并运行Rspec安装生成器:
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): 现在编辑
spec/spec_helper.rb
(或rails 4+中的spec/rails_helper.rb
,不确定旧版本)更改此行(第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. 现在您可以从插件的根目录运行Rspec,它也将从虚拟应用程序中获取规范。
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: 我更详细地写了这篇文章,展示了如何在带有虚拟应用程序的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 https://web.archive.org/web/20130125091106/http://namick.tumblr.com/post/17663752365/how-to-create-a-gemified-plugin-with-rails-3-2-rspec
Just run rails plugin new <gemname>
and then add rspec as development_dependency to gemspec file, and install it rspec --init
. 只需运行
rails plugin new <gemname>
,然后将rspec作为development_dependency添加到gemspec文件,并将其安装到rspec --init
。
Now move dummy sub folder from test
to spec
and add these to spec_helper
: 现在将dummy子文件夹从
test
移动到spec
并将它们添加到spec_helper
:
ENV["RAILS_ENV"] = "test"
require File.expand_path("../dummy/config/environment.rb", __FILE__)
require "rails/test_help"
require '<gemname>'
Rails.backtrace_cleaner.remove_silencers!
as they are in test_helper
! 因为他们在
test_helper
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.