简体   繁体   English

Rails 3.1插件gem,虚拟测试应用程序,rspec

[英]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.

相关问题 在编写gem时如何测试虚拟rails应用程序? - How do you test a dummy rails app when writing a gem? rails:如何使用gem提供模型,以及如何在没有虚拟app的情况下测试rails gem - rails: how to supply a model with gem, and how to test rails gem without dummy app Rork引擎测试/假人与Spork,Guard,RSpec? - Rails engine test/dummy with Spork, Guard, RSpec? 如何防止 RSpec 在带有虚拟应用程序的 Rails 插件中运行规范两次? - How to prevent RSpec from running specs twice in Rails plugin with dummy app? 使用虚拟应用程序测试Rails 4 Engine - Test a Rails 4 Engine with the dummy app Rails3.1引擎:无法使SLIM或HAML在测试/虚拟应用程序中工作 - Rails3.1 engine: can't get SLIM or HAML to work in test/dummy app rspec测试使用Rails应用程序数据库中整个表的伪数据 - rspec test that uses dummy data from an entire table in rails app database 使用测试/虚拟应用程序测试Rails gem,/ tmp文件夹会崩溃……以某种方式 - Testing a Rails gem with test/dummy app, /tmp folder gets clobbered…somehow 如何从gem配置Rails 3.1应用程序? - How to configure Rails 3.1 app from a gem? Rails 3.1 Rspec Rails 2-如何测试此控制器操作? - Rails 3.1 Rspec Rails 2 - How can I test this controller action?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM