Skip to content

Instantly share code, notes, and snippets.

@cmkoller
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmkoller/bdd82c49df57f78a02ae to your computer and use it in GitHub Desktop.
Save cmkoller/bdd82c49df57f78a02ae to your computer and use it in GitHub Desktop.
Setting up a Rails app

#Rails Set-Up Cheat Sheet

For a quick Rails set-up, check out the make_it_so gem! It will generate a Rails project with all this built in and more. (User authentication included!) However, if you're like me and prefer to step through the process on your own first, this is the cheat sheet to remember how to set things up!

Protip: Any time you would type rails generate in your command line, you can type rails g instead!

###Creating the app

In command line:

rails new <name> --database=postgresql -T
cd <name>
rake db:create

In Gemfile:

group :test, :development do 
  gem 'pry-rails'
  gem 'launchy'
  gem 'quiet_assets'
end

In command line:

bundle install

###Adding RSpec

In Gemfile:

group :test, :development do 
 # ... other gems ... 
  gem 'rspec-rails'
  gem 'capybara'
end

In command line:

bundle install
rails generate rspec:install

###Setting up your tests

Create /spec/features. For each feature, create a file: /spec/features/<test>_spec.rb.

Here's a sample, for a feature about listing all users on an index page:

require ‘rails_helper’

# brief description of this test file, and a user story
feature "View All Users", %q(
  As a visitor to the site
  I want to view a list of all users
  So I can see who else is cool enough to use this app.
) do

  # variables you'll be using in most of your tests
  let(:user){ User.create(name: "Christina") }
  
  before(:each) do
    # stuff you want to do before each test
    sign_in_as(:user)
  end
  
  # the actual test!
  scenario '<scenario>' do
    visit users_path
    
    expect(page).to have_content (user.name)
  end
end

Create /spec/models. For each model, create a file: /spec/features/<model_name>_spec.rb:

require "rails_helper"

describe User do
  
  # define the variables you'll be using in your tests
  let(:user) { User.create(first_name: "Christina", last_name: "Koller") } 
  
  # for each method you write in your model, make a test here!
  describe "#full_name" do
    it "adds together the user's first and last name" do
      expect(user.full_name).to eq("Christina Koller")
    end
  end
end

###Adding Foundation

In Gemfile:

gem 'foundation-rails'

In command line:

rails generate foundation:install

###Creating Migrations

rails generate migration <migraiton_name>

###Allowing the focus: true statement in tests

Are you getting frustrated while waiting for all your tests to run, when all you really care about is the test you're working on right now? Adding focus: true to your test will allow you to run only that test, and save a lot of time! First you have to set it up. Here's how:

  1. Go to spec_helper.rb
  2. Delete =begin and =end from the file
  3. Comment out:
  • config.profile_examples = 10
  • config.disable_monkey_patching!

You can now add focus: true to your tests like this:

scenario "all users are listed on the page", focus: true do

end

When you're done just running one test, delete the focus: true and all your tests will run again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment