All you need to know about writing a mountable Rails Gem with rspec
by Yuva, Co-founder
This article briefly talks about how to write a mountable rails gem and what are the practices that I followed
First things first
Rails generates nice boilerplate for your gem which is sometimes hard to get it right in the first go. Use this simple command and you are good to go
$ rails plugin new <gem-name> --mountable --skip-test-unit --dummy-path=spec/dummy
- The
--mountableoption helps in creating a mountable gem. - The
--skip-test-unitskips generation of test templates intestfolder - The
--dummy-pathspecifies where the dummy test application (which we will be using for testing) should be generated. It is usually created intestfolder but you want that to be created inspecfolder so that you can use rspec
Importance of dummy app
Some of the gems that you develop which depend on rails need to run specs in rails environment. Since gem doesn't have a rails env, it uses dummy app's rails env to run such tests. This is useful when you write specs for controllers, helpers etc.
When you set up rspec by adding rspec-rails to your Gemfile, you need to add
these lines at the top of spec_helper.rb so that rspec loads rails env of dummy
app.
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../dummy/config/environment", __FILE__)
General flow of development
-
Never checkin
Gemfile.lockin your git repository{or whatever SCM that you are using} -
First, add all the dependent gems that your gem needs in Gemfile and develop your gem.
-
Once you have finalized your dependent gems, move those gem declarations to
gemspecfile. Add all dependent gems withadd_dependencymethod and add all development dependent gems withadd_development_dependencymethodGem::Specification.new do |s| # your information s.add_dependency "strong_parameters", "~> 0.2.1" s.add_development_dependency "factory_girl_rails" end -
Its important to note that you should require your dependent gems explicitly in the root file of your gem. Say if your gem is named
my_cool_gem, then you should havemy_cool_gem.rbcreated insidelibfolder. If your gem is dependent onstrong_parameters, then you need to add these lines:require "my_cool_gem/engine" require "strong_parameters" module MyCoolGem end