Skip to content

Instantly share code, notes, and snippets.

@ryanflach
Last active November 13, 2023 19:49
Show Gist options
  • Save ryanflach/9fe657471bc9282a18d6904171645278 to your computer and use it in GitHub Desktop.
Save ryanflach/9fe657471bc9282a18d6904171645278 to your computer and use it in GitHub Desktop.
Common setup for a new Rails project
  1. rails new <project_name> -d postgresql --skip-turbolinks --skip-spring -T
  • -d postgresql sets up the project to use PostgreSQL
  • --skip-turbolinks & --skip-spring creates a project that does not use turbolinks or spring
  • -T skips the creation of the test directory and use of Test::Unit
  1. In the Gemfile:
  • Available to all environments:
    • gem 'figaro' - store environment variables securely across your app (docs)
    • Uncomment gem 'bcrypt', '~> 3.1.7' if you will be hosting your own user accounts with passwords (docs)
  • Inside of group :test:
    • gem 'rspec-rails' - user rspec in place of minitest (docs)
    • gem 'capybara' - act as a user navigating your site in tests (docs)
    • gem 'launchy' - for running save_and_open_page (docs)
    • gem 'shoulda-matchers' - easier model testing (docs)
    • gem 'database_cleaner' - clean your database between tests (docs)
    • gem 'factory_girl_rails' - easily generate database objects without repetitive code in tests (docs)
    • gem 'simplecov', require: false - generate reports on your test coverage (docs)
  • inside of group :production:
    • gem 'rails_12factor', group: :production - If publishing on Heroku (docs)
  1. Bundle and install (bundle exec or rails g) where required, create additional required files
  • bundle
  • rails g rspec:install
  • bundle exec figaro install
  • mkdir spec/support
  • touch spec/support/factory_girl.rb
  • touch spec/support/factories.rb
  • touch spec/support/database_cleaner.rb
  1. Add config data
  • In spec/rails_helper.rb
    require 'capybara/rails'
    
    Shoulda::Matchers.configure do |config|
      config.integrate do |with|
        with.test_framework :rspec
        with.library :rails
      end
    end
    
    • Uncomment Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
    • Change config.use_transactional_fixtures = true to ... = false
  • In spec/support/factory_girl.rb
    RSpec.configure do |config|
      config.include FactoryGirl::Syntax::Methods
    end
    
    
  • In spec/support/factories.rb
    FactoryGirl.define do
      <factories for each model go here>
    end
    
    
  • In spec/spec_helper.rb
    • Note: this must appear at the very top of this file
    require 'simplecov'
    SimpleCov.start 'rails'
    
  • In spec/support/database_cleaner.rb
    RSpec.configure do |config|
    
      config.before(:suite) do
        DatabaseCleaner.clean_with(:truncation)
      end
    
      config.before(:each) do
        DatabaseCleaner.strategy = :transaction
      end
    
      config.before(:each, :js => true) do
        DatabaseCleaner.strategy = :truncation
      end
    
      config.before(:each) do
        DatabaseCleaner.start
      end
    
      config.after(:each) do
        DatabaseCleaner.clean
      end
      
      # Optional for formatting output of test suite runs (see comments)
      config.formatter = :documentation
    
    end
    
  • In .gitignore
    # Ignore SimpleCov files
    coverage
    
    
  1. (Optional) If working with an external API:
  • In Gemfile
    • Available to all environments
      • gem 'faraday' - for making http requests (docs)
    • under group :test
      • gem 'vcr' - for stubbing (docs)
      • gem 'webmock' - for mocking (used by VCR) (docs)
  • bundle
  • In spec/rails_helper.rb
    require 'vcr'
    
    VCR.configure do |config|
      config.cassette_library_dir = "spec/vcr_cassettes"
      config.hook_into :webmock
    end
    
  • In .gitignore
    # Ignore vcr cassettes
    /spec/vcr_cassettes
    
    
  1. (Optional) If you will be using feature tests that utilize JavaScript:
  • In Gemfile
    • under group :test
    gem 'selenium-webdriver`
    
    note: Firefox 46 is required for Selenium
  1. (Optional) If you will be using ES6 syntax:
  • In 'Gemfile'
    • available to all environments:
      • gem 'sprockets', '>= 3.0.0'
      • gem 'sprockets-es6' - Enable use of ES6 syntax in production (useful if pushing to Heroku) (docs)
    • In application.rb:
      • require 'sprockets/es6'
    • Any JS files that use ES6 syntax will need to be saved as .es6
@kbs5280
Copy link

kbs5280 commented Sep 20, 2016

I think simplecov is missing and 'e' in:

require 'simplcov'
SimpleCov.start 'rails'

@jesse-spevack
Copy link

This is awesome.

@ryanflach
Copy link
Author

@kbs5280: Great eye. Fixed.

@jesse-spevack
Copy link

For rspec setup I like the documentation view of the test suite:

  • under RSpec.configure do |config| add
    config.formatter = :documentation

@ryanflach
Copy link
Author

@PlanetEfficacy Great suggestion. I've never seen that output. Want to share a screenshot?

@jesse-spevack
Copy link

screen shot 2016-10-31 at 8 33 26 am
@ryanflach documentation mode just prints out the names of the tests instead of "." for each test. I like it :)

@ryanflach
Copy link
Author

@PlanetEfficacy Thanks. I've added it as an optional part of the RSpec config block.

@jesse-spevack
Copy link

Another flag I'll be adding in future rails apps.... --skip-action-cable unless I plan to use web sockets. @ryanflach

@alishersadikov
Copy link

A lot of useful setup notes in one place, thanks!

@jesse-spevack
Copy link

@ryanflach I recommend adding the following bullet point to number 1:

  • Optional: --api will create a stripped-down Rails application with a smaller set of middleware. It will also configure the generators to skip the views/helpers/assets and change the parent for ApplicationController from ActionController::Base to ActionController::API

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