#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:
- Go to spec_helper.rb
- Delete
=begin
and=end
from the file - 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!