Skip to content

Instantly share code, notes, and snippets.

@jsheridanwells
Created January 17, 2018 23:39
Show Gist options
  • Save jsheridanwells/f0337dd52efa508ec946c302a9a08c56 to your computer and use it in GitHub Desktop.
Save jsheridanwells/f0337dd52efa508ec946c302a9a08c56 to your computer and use it in GitHub Desktop.
Connecting Users to Posts :: Rspec and Capybara setup :: Devise (from Udemy Professional TDD Rails App tutorial)
# add devise gem to Gemfile.rb, go to RubyGems to get latest version #
gem 'devise'
# Build Devise
rails g devise:install
# Add correct mailer in development.rb, config method will appear in terminal output after installing
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
# Generate Devise views
$ rails g devise:views
# Generate Devise User model
$ rails g devise User first_name:string last_name:string email:string
# Migrate
$ rails db:migrate
# generate rails app w/ postgresql database, skip MiniTest
$ rails new DeviseApp --database=postgresql -t
# Add Rspec, Capybara, and DatabaseCleaner to Gemfile.rb, add to :development, :test group
gem 'capybara', '~> 2.13'
gem 'rspec-rails', '~> 3.7.2'
gem 'database_cleaner'
# install Rspec
$ rails g rspec:install
# Add test config settings in rails_helper.rb
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rails'
include Warden::Test::Helpers
Warden.test_mode!
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.before(:suite) { DatabaseCleaner.clean_with(:truncation) }
config.before(:each) { DatabaseCleaner.strategy = :transaction }
config.before(:each, :js => true) { DatabaseCleaner.strategy = :truncation }
config.before(:each) { DatabaseCleaner.start }
config.after(:each) { DatabaseCleaner.clean }
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
# Integrations tests: Set up features test folder
$ mkdir spec/features
# create static page spec (template for further integration tests) . ::Fails
$ touch spec/features/static_spec.rb
# create test in static_spec.rb
require 'rails_helper'
describe 'navigate' do
describe 'homepage' do
it 'can be reached successfully' do
visit root_path
expect(page.status_code).to eq(200)
end
end
end
# create database ::Fails
$ rails db:create
$ rails db:migrate
# Create route for home page in routes.rb . ::Fails
root to: 'static#homepage'
# Create controller ::Fails
$ touch app/controllers/static_controller.rb
# Create homepage method in static_controller.rb. ::Fails
class StaticController < ApplicationController
def homepage
end
end
# create homepage view ::Passes
$ touch app/views/static/homepage.html.erb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment