My Rails New Workflow With Testing

After playing around with Django a few weekends ago, I decided to stick to Rails for my personal projects. This Quora answer sums up pretty well why I’m choosing Rails.

Another reason I love Rails is because I love test driven development, and Rails has testing at it’s core. I started a new project this morning, and wanted to share my workflow for starting a Rails app with all the Testing gems and configurations set up properly:

make your rails app

$ rails new [APPNAME] --skip-test-unit --skip-bundle -d postgresql
$ cd [APPNAME]

Note: skip the “-d postgresql” part if you’re sticking with sqlite.

configure rvm

$ rvm list
$ rvm use current
$ rvm --create --rvmrc 1.9.3@yourappname

If your current ruby version is old, feel free to use a different one. If you’d like to learn more about rvm, check out this rvm tutorial.

open app in sublime

$ subl .

If you’re using Textmate, you can do “mate .”. You can also just open the project manually via whatever Text Editor you’re using.

configure postgresql

go to config/database.yml
1. change username to [YOUR_USERNAME]
2. add “host: localhost” to the test and development sections

If you’re using sqlite, skip this step. If you’d like to get Postgres, check out my post on how to download and set up Postegresql on your computer.

configure gemfile

open your Gemfile
1. review your past gem files, and copy as necessary. Here is an example of a current Gemfile I’m using as my template. Also, here is a list of my favorite gems for testing.

$ bundle

create your databases

$ rake db:create:all
$ rake db:migrate

set up rspec

$ rails generate rspec:install 

add defaults to rspec

 
$ subl .rspec

add:
– – color
– – drb

adding guard and spork

$ bundle exec spork --bootstrap

go into spec/helper
— cut everything from:
ENV[“RAILS_ENV”] ||= ‘test’
require File.expand_path(“../../config/environment”, __FILE__)
require ‘rspec/rails’
require ‘rspec/autorun’
..
–and paste it into the Spork.prefork section (up top!)

make sure it works…

$ time bundle exec rspec 

make sure Spork works…

$ bundle exec spork

initialize guard with spork and rspec

$ bundle exec guard init spork
$ guard init rspec

to run guard…

$ bundle exec guard

** but we don’t actually want to run it now

time to install capybara

1. create integration folder in spec folder
2. in /spec_helper.rb add:
require ‘capybara/rspec’
3. create first test, make sure name has _spec in the filename
ex: signin_spec.rb
4. require ‘spec_helper’ in capybara test

For example:

require 'spec_helper'
describe 'user creation' do
  it 'allows a user to sign in via twitter' do
   visit '/users/sign_in'
   page.should have_button 'Sign In With Twitter'
  end
end

adding simple cov (test coverage!)

add this TO THE TOP of /spec_helper.rb

require 'simplecov'
SimpleCov.start 'rails'

setting up fabricators

open config/application.rb and put this under Application

config.generators do |g|
  g.test_framework      :rspec, :fixture => true
  g.fixture_replacement :fabrication
end

… and add the folder “fabricators” to your spec folder
so you’ll have ..spec/fabricators

change README file

$ mv README.rdoc README.md
$ subl README.md

Add the description of your application.

commit to git

$ git init
$ git add .
$ git commit -m "initial commit"

Go to github, create new repository. Copy the last 2 lines of what github lists the next steps are:

$ git remote add origin git@github.com:YOURUSERNAME/YOURPROJECT.git
$ git push -u origin master

push to heroku

$ heroku create
$ heroku rename YOURFUNAPPNAME
$ git push heroku master

Enjoy the article? Join over 14,500+ Swift developers and enthusiasts who get my weekly updates.