Ninja Rails Developer

 Date: July 23, 2013

Warning: this configuration was tested only on MacOS X.

To create default project in Rails we just need to run command:

rails new my_app

However it creates project without many useful gems. I found nice Gem-set and some configuration hacks in Michael Hartl's tutorial.

Instead of standard test framework (TestUnit) he use RSpec (which is more popular among Rails developers). To generate project without standard test, run command:

rails new my_app --skip-test-unit

Then use his Gemfile (this link is kept up to date). Copy it a paste into my_app/Gemfile. I recommend to uncomment everything (to use all Gems from the file).

Once Gemfile is modified, you need to install gems:

bundle install

Then you can run RSpec test:

bundle exec rspec

You may need to point the tests' directory:

bundle exec rspec rspec/

It would be nice to run them with just 'rspec' command. To do that we need to run following commands (if we used RVM to install Rails):

rvm get head && rvm reload

cd ~/projects/my_app

bundle install --without production --binstubs=./bundler_stubs

After those steps you should be able to run RSpec with:

rspec

You should get a warning, that to maintain this possibility you need to add following line to ~/.bash_profile if it exists, otherwise add it to ~/.bash_login:

source ~/.profile

Michael Hartl's Gem file contains 'guard'. It allows us to run RSpec test each time, when we change some file (after save).

To initialize guard (if all above steps are performed, otherwise you need to add 'bundle exec ' at the beginning):

guard init

It creates Guardfile in main project directory, which specify when tests should be run (which files should be monitored). To run guard:

guard

The last improvement is use the test server Spork. It allows to run tests faster. Without Spork, RSpec needs to reload entire Rails environment before run the tests (in each run). Spork loads the environment once, and then maintains a pool of processes for running future tests. Unfortunately it works only on POSIX systems (which means: doesn't work on Windows).

To setup spork run:

spork --bootstrap

Then change spec/spec_helper.rb file to something like that:

require 'rubygems'
require 'spork'
Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  RSpec.configure do |config|
    config.mock_with :rspec
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.infer_base_class_for_anonymous_controllers = false
  end
end
Spork.each_run do
  # This code will be run each time you run your specs.
end

You also need to configure RSpec to automatically use Spork. To do that, modify .rspec file:

--color
--drb

To run Spork:

spork

You can test difference between running tests with and without spork.

time rspec

In my case it was 4.821s without Spork, and 0.784 with Spork.

The disadvantage of Spork is that after e.g. changes in database we need to restart it.

The screencast showing advanced setup by Michael Hartl is available for free here. If you use SublimeText (as I do), you can follow this screencast to adjust it for Rails development. After that you will be able even to run tests from SublimeText!

 Tags:  programming tools

Previous
⏪ Sign in with facebook (OAuth): how to and threats

Next
The Ten-Day MBA ⏩