Rails 3.1 plugin gem, dummy test app, rspec

扶醉桌前 提交于 2019-11-29 18:57:18
nathan amick

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

Just run rails plugin new <gemname> and then add rspec as development_dependency to gemspec file, and install it rspec --init.

Now move dummy sub folder from test to spec and add these to 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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!