My Rails New Workflow With Testing

21 Sep

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

– – 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'

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

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

change README file

$ mv README.rdoc
$ subl

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 push -u origin master

push to heroku

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

One Response to “My Rails New Workflow With Testing”

  1. SutoCom September 22, 2012 at 12:36 pm #

    Reblogged this on Sutoprise Avenue, A SutoCom Source.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s