Skip to content

Instantly share code, notes, and snippets.

@rsepassi
Created September 25, 2013 18:15
Show Gist options
  • Save rsepassi/6703703 to your computer and use it in GitHub Desktop.
Save rsepassi/6703703 to your computer and use it in GitHub Desktop.
RSpec Outline

RSpec

Day 1: Testing Models + API

  • RSpec

Project Requirements

  • Associations + validations
  • Scopes
  • A few custom model methods
  • JSON API
  • A few complex routes

Day 2: Integration Testing

  • RSpec + Capybara

Project Requirements

  • Maybe build off Day 1's project
  • Add views
  • Sign up, login, logout
  • Multiple high-level user features

Topics

  • Why test? refresher
  • Syntax refresher

What to test?

  • Models + Integration

  • Models

    • Validations
    • Associations
    • Scopes
    • Important methods
  • Integration (i.e. Features)

    • Sign up, login, logout
    • Any and all important features
  • API

    • Test JSON responses w/ rspec (no Capybara)

Rails + rspec setup

Code:

rails new MyApp -T (excludes default test framework)
OR rm -rf test/ (if you already have the default test framework setup)
group :development, :test do
  gem 'rspec-rails'
end
bundle install
rails generate rspec:install
  • specs will be generated automatically anytime you use a rails generator

.rspec file:

--format documentation
  • Capybara

Gemfile: gem 'capybara' spec_helper.rb: require 'capybara/rspec'

  • spec/ folder structure
models/
controllers/
features/
spec_helper.rb
  • What is integration testing? Feature testing

    • Visit the site, do X, Y, and Z, check to make sure it worked
  • Test database

    • Clears each time automatically
    • rake db:test:prepare:
      • Checks for pending migrations
      • Loads schema into test database
  • Other supporting gems:

gem 'factory_girl_rails'
gem 'guard-rspec'
gem 'faker'
  • Faker:

spec/factories/contacts.rb:

require 'faker'

FactoryGirl.define do
  factory :contact do |f|
    f.firstname { Faker::Name.first_name }
    f.lastname { Faker::Name.last_name }
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment