How to add RSpec testing to your Rails application.

First things first, Setup:

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'rspec-rails'
end
# Download and install
$ bundle install
# Generate boilerplate configuration files
# (check the comments in each generated file for more information)
$ rails generate rspec:install
create .rspec
create spec
create spec/spec_helper.rb
create spec/rails_helper.rb
group :development, :production do
gem 'pg', '~> 1.1'
end
group :test do
gem 'sqlite3'
end
test:
adapter: sqlite3
database: testdb
config.active_record.maintain_test_schema = true
rails db:create RAILS_ENV=test
rails db:migrate RAILS_ENV=test

Writing specs:

Routing

require "rails_helper"RSpec.describe "Routes", :type => :routing do# tests will go hereend
require "rails_helper"RSpec.describe "Routes", :type => :routing doit "route '/' to point root page, application#welcome"it "route /google_auth to point to the sessions#google_auth"it "route /logged_in to point to the sessions#is_logged_in?"it "route /logout to point to the sessions#destroy"end
it "route '/' to point root page, application#welcome" doexpect(get("/")).to route_to("application#welcome")expect(get("/")).to be_routableend
it "route /get_menu/:id to point to the users#get_menu" doexpect(get("get_menu/1")).to route_to(controller: "users", action:  'get_menu', id: '1')expect(get("/get_menu/1")).to be_routableend

Models

rails generate rspec:model user

Controllers

file = fixture_file_upload('test.pdf', 'application/pdf')
spec/fixtures/files/test.pdf