Skip to content

Commit

Permalink
chore: add rspec for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ct-wu committed Jul 7, 2022
1 parent 59f5bfa commit 2f71665
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 43 deletions.
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--require spec_helper
11 changes: 2 additions & 9 deletions Gemfile
Expand Up @@ -37,6 +37,7 @@ gem 'jbuilder', '~> 2.5'
gem 'bootsnap', '>= 1.1.0', require: false

group :development, :test do
gem 'rspec-rails'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
Expand All @@ -50,15 +51,7 @@ group :development do
gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'mongoid', '7.4.0'
gem 'mongoid', '7.4.0'
53 changes: 19 additions & 34 deletions Gemfile.lock
Expand Up @@ -42,30 +42,13 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
archive-zip (0.12.0)
io-like (~> 0.3.0)
arel (9.0.0)
bindex (0.8.1)
bootsnap (1.12.0)
msgpack (~> 1.2)
bson (4.15.0)
builder (3.2.4)
byebug (11.1.3)
capybara (3.37.1)
addressable
matrix
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
childprocess (4.1.0)
chromedriver-helper (2.1.1)
archive-zip (~> 0.10)
nokogiri (~> 1.8)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
Expand All @@ -75,14 +58,14 @@ GEM
coffee-script-source (1.12.2)
concurrent-ruby (1.1.10)
crass (1.0.6)
diff-lcs (1.5.0)
erubi (1.10.0)
execjs (2.8.1)
ffi (1.15.5)
globalid (1.0.0)
activesupport (>= 5.0)
i18n (1.10.0)
concurrent-ruby (~> 1.0)
io-like (0.3.1)
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
Expand All @@ -96,7 +79,6 @@ GEM
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.8.0)
Expand All @@ -112,7 +94,6 @@ GEM
nokogiri (1.13.6)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
public_suffix (4.0.7)
puma (3.12.6)
racc (1.6.0)
rack (2.2.4)
Expand Down Expand Up @@ -146,11 +127,25 @@ GEM
rb-fsevent (0.11.1)
rb-inotify (0.10.1)
ffi (~> 1.0)
regexp_parser (2.5.0)
rexml (3.2.5)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-rails (5.1.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
railties (>= 5.2)
rspec-core (~> 3.10)
rspec-expectations (~> 3.10)
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.11.0)
ruby2_keywords (0.0.5)
ruby_dep (1.5.0)
rubyzip (2.3.2)
sass (3.7.4)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
Expand All @@ -162,11 +157,6 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
selenium-webdriver (4.3.0)
childprocess (>= 0.5, < 5.0)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
spring (2.1.1)
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
Expand Down Expand Up @@ -194,29 +184,24 @@ GEM
activemodel (>= 5.0)
bindex (>= 0.4.0)
railties (>= 5.0)
websocket (1.2.9)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)

PLATFORMS
ruby

DEPENDENCIES
bootsnap (>= 1.1.0)
byebug
capybara (>= 2.15)
chromedriver-helper
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
mongoid (= 7.4.0)
puma (~> 3.11)
rails (~> 5.2.8)
rspec-rails
sass-rails (~> 5.0)
selenium-webdriver
spring
spring-watcher-listen (~> 2.0.0)
sqlite3
Expand Down
55 changes: 55 additions & 0 deletions spec/rails_helper.rb
@@ -0,0 +1,55 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

RSpec.configure do |config|
# Remove this line to enable support for ActiveRecord
config.use_active_record = false

# If you enable ActiveRecord support you should unncomment these lines,
# note if you'd prefer not to run each example within a transaction, you
# should set use_transactional_fixtures to false.
#
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
# config.use_transactional_fixtures = true

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, type: :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!

# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
131 changes: 131 additions & 0 deletions spec/requests/users_spec.rb
@@ -0,0 +1,131 @@
require 'rails_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to test the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.

RSpec.describe "/users", type: :request do

# This should return the minimal set of attributes required to create a valid
# User. As you add validations to User, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}

let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}

describe "GET /index" do
it "renders a successful response" do
User.create! valid_attributes
get users_url
expect(response).to be_successful
end
end

describe "GET /show" do
it "renders a successful response" do
user = User.create! valid_attributes
get user_url(user)
expect(response).to be_successful
end
end

describe "GET /new" do
it "renders a successful response" do
get new_user_url
expect(response).to be_successful
end
end

describe "GET /edit" do
it "renders a successful response" do
user = User.create! valid_attributes
get edit_user_url(user)
expect(response).to be_successful
end
end

describe "POST /create" do
context "with valid parameters" do
it "creates a new User" do
expect {
post users_url, params: { user: valid_attributes }
}.to change(User, :count).by(1)
end

it "redirects to the created user" do
post users_url, params: { user: valid_attributes }
expect(response).to redirect_to(user_url(User.last))
end
end

context "with invalid parameters" do
it "does not create a new User" do
expect {
post users_url, params: { user: invalid_attributes }
}.to change(User, :count).by(0)
end

it "renders a successful response (i.e. to display the 'new' template)" do
post users_url, params: { user: invalid_attributes }
expect(response).to be_successful
end
end
end

describe "PATCH /update" do
context "with valid parameters" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}

it "updates the requested user" do
user = User.create! valid_attributes
patch user_url(user), params: { user: new_attributes }
user.reload
skip("Add assertions for updated state")
end

it "redirects to the user" do
user = User.create! valid_attributes
patch user_url(user), params: { user: new_attributes }
user.reload
expect(response).to redirect_to(user_url(user))
end
end

context "with invalid parameters" do
it "renders a successful response (i.e. to display the 'edit' template)" do
user = User.create! valid_attributes
patch user_url(user), params: { user: invalid_attributes }
expect(response).to be_successful
end
end
end

describe "DELETE /destroy" do
it "destroys the requested user" do
user = User.create! valid_attributes
expect {
delete user_url(user)
}.to change(User, :count).by(-1)
end

it "redirects to the users list" do
user = User.create! valid_attributes
delete user_url(user)
expect(response).to redirect_to(users_url)
end
end
end
38 changes: 38 additions & 0 deletions spec/routing/users_routing_spec.rb
@@ -0,0 +1,38 @@
require "rails_helper"

RSpec.describe UsersController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(get: "/users").to route_to("users#index")
end

it "routes to #new" do
expect(get: "/users/new").to route_to("users#new")
end

it "routes to #show" do
expect(get: "/users/1").to route_to("users#show", id: "1")
end

it "routes to #edit" do
expect(get: "/users/1/edit").to route_to("users#edit", id: "1")
end


it "routes to #create" do
expect(post: "/users").to route_to("users#create")
end

it "routes to #update via PUT" do
expect(put: "/users/1").to route_to("users#update", id: "1")
end

it "routes to #update via PATCH" do
expect(patch: "/users/1").to route_to("users#update", id: "1")
end

it "routes to #destroy" do
expect(delete: "/users/1").to route_to("users#destroy", id: "1")
end
end
end

0 comments on commit 2f71665

Please sign in to comment.