Skip to content

Instantly share code, notes, and snippets.

@julienma
Last active December 19, 2015 16:59
Show Gist options
  • Save julienma/5987378 to your computer and use it in GitHub Desktop.
Save julienma/5987378 to your computer and use it in GitHub Desktop.
Properly bootstrapping a new rails app, including RSpec + Spork + Foreman + Heroku

All these useful tips (and more!) taken from http://ruby.railstutorial.org/chapters/

This is based on Rails 3.2, but should work on Rails 4.0.

Create Rails app

(skip standard test unit and instead will use RSpec)

$ cd ~/rails_projects
$ rails new MyApp --skip-test-unit
$ cd MyApp

Add useful gems

Edit local ./Gemfile and make sure to include important gems (rspec, capybara, spork)

Then install new gems

$ bundle update
$ bundle install

Setup RSpec to work with generate

$ rails generate rspec:install

Setup Spork

$ bundle exec spork --bootstrap
  1. Edit ./spec/spec_helper.rb and move all environment (at the end of the file) in the 'prefork' block. Also include Capybara DSL.

  2. Edit ./.rspec and add '--drb'

Quick start with Foreman

  1. Create a ./Procfile

  2. Start everything (spork + rails server)

$ bundle exec foreman start

Git it

$ git init
$ git add .
$ git commit -m "Initialize repository"

Setup remote - Depends on server (github, bitbucket, etc.)

$ git remote add origin git@github.com:<username>/myapp.git

Then push to remote

$ git push -u origin master

Deploy to Heroku

$ heroku create
$ git push heroku master
$ heroku open
$ heroku logs

Now do some stuff

Like...

$ rails generate controller MyController user task --no-test-framework

or

$ rails g model MyModel content:string model_id:integer

Can also undo 'generate' actions - need to use the exact same arguments than with 'generate'

$ rails destroy controller MyController user task

Manually generate tests

$ rails generate integration_test mycontroller_pages

And run them manually

$ bundle exec rspec spec/

Issues when testing

Do not forget to prepare test DB and restart Spork (especially when updating routes or doing migrations)

$ bundle exec rake db:migrate
$ bundle exec rake db:test:prepare

...and stop Spork (CTRL+C) and restart it:

$ bundle exec foreman start

Execute tests from within Sublime Text 2

Needs https://github.com/maltize/sublime-text-2-ruby-tests

If using RVM, might also needs to set "check_for_rvm": true, in '~/Library/Application\ Support/Sublime\ Text\ 2/Packages/RubyTest/RubyTest.sublime-settings'

Then from ST2, use CMD-Shift-R/T/E shortcuts to start tests

Once in a while, run rspec spec/ from CLI to confirm that the entire test suite is still green

Misc useful stuff

Output parameters in a view (in dev only)

<%= debug(params) if Rails.env.development? %>

--color
--drb
source 'https://rubygems.org'
gem 'rails', '3.2.13'
.
.
.
group :development, :test do
.
.
.
gem 'sqlite3' # dev DB
gem 'rspec-rails' # automated testing
gem 'capybara' # automated testing
gem 'spork' # automated testing
gem 'foreman' # Procfile. Fix an issue with Foreman trying to use system Ruby (1.8).
end
group :production do
gem 'pg' # for Heroku deployment
end
web: bundle exec rails server -p $PORT
spork: bundle exec spork
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.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Capybara::DSL # Needed for Rails 4.0 - Should work with 3.2
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment