Skip to content

Instantly share code, notes, and snippets.

@jonathanroehm
Last active March 27, 2019 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanroehm/054598fef77543d2f8e8a492b579574d to your computer and use it in GitHub Desktop.
Save jonathanroehm/054598fef77543d2f8e8a492b579574d to your computer and use it in GitHub Desktop.
How to add `spring` to your rails plugin / engine for use with RSpec and beyond.
# In `your_engine/Gemfile` add the pertinent spring gems:
# Note, do not add spring gems as `spec.add_development_dependency`'s in your gemspec.
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gemspec
group :development do
gem 'spring'
gem 'spring-watcher-listen'
end
# If you're using rspec:
group :test do
gem 'spring-commands-rspec'
end
# Add `spring.rb` to your engine's config folder:
# `your_engine/config/spring.rb`
#
# when generating a plugin via `rails plugin new your_engine`
# (without customizing the dummy_app path)
# a dummy rails app is created.
#
# If you're using RSpec, you'll path will look like: './spec/dummy'
Spring.application_root = './test/dummy'
@jonathanroehm
Copy link
Author

jonathanroehm commented Mar 27, 2019

Using spring binstubs in the terminal

Remember to re-bundle your gem from the gem's root directory once you've made these changes. The root Gemfile will automatically be applied to the dummy app.

When using RSpec, you can spring your tests via:

$ spring rspec spec/features/my_feature_spec.rb

Or, in entirety:

$ spring rspec

How to generate your plugin so it's ready for RSpec

You can also utilize RSpec's spec/ folder for placement of your dummy app by adding the following flags to your generation:

  • --skip-test
  • --skip-system-test
  • --dummy-path=spec/dummy

Example:

$ rails plugin new your_plugin_name --skip-test --skip-system-test --dummy-path=spec/dummy

Then, you simply add rspec-rails as a development dependency, re-bundle, then run the RSpec installation generator:

# in your_engine.gemspec
...
Gem::Specification.new do |spec|
  ...
  spec.add_development_dependency 'rspec-rails'
end
$ rails generate rspec:install

Good to go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment