Skip to content

Commit

Permalink
Add testing frameworks for unit and integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
kwizl committed May 27, 2020
1 parent e069ae6 commit 28dd1a1
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 56 deletions.
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ gem 'devise'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: %i[mri mingw x64_mingw]
gem 'rspec-rails', '~> 4.0.0'
gem 'factory_bot_rails', '~> 5.2'
gem 'rubocop-rails'
# Capybara, the library that allows us to interact with the browser using Ruby
gem 'capybara', '>= 2.15'
gem 'shoulda-matchers'
# The following gems aids with the nuts and bolts
# of interacting with the browser.
gem 'database_cleaner', '~> 1.8', '>= 1.8.5'
end

group :test do
Expand Down
40 changes: 40 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
arel (9.0.0)
ast (2.4.0)
bcrypt (3.1.13)
Expand All @@ -50,6 +52,14 @@ GEM
msgpack (~> 1.0)
builder (3.2.4)
byebug (11.1.1)
capybara (3.32.2)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (~> 1.5)
xpath (~> 3.2)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
Expand All @@ -59,6 +69,7 @@ GEM
coffee-script-source (1.12.2)
concurrent-ruby (1.1.5)
crass (1.0.6)
database_cleaner (1.8.5)
devise (4.7.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
Expand All @@ -68,6 +79,11 @@ GEM
diff-lcs (1.3)
erubi (1.9.0)
execjs (2.7.0)
factory_bot (5.2.0)
activesupport (>= 4.2.0)
factory_bot_rails (5.2.0)
factory_bot (~> 5.2.0)
railties (>= 4.2.0)
ffi (1.12.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -101,6 +117,7 @@ GEM
parser (2.7.0.2)
ast (~> 2.4.0)
pg (1.2.2)
public_suffix (4.0.5)
puma (3.12.2)
rack (2.1.2)
rack-test (1.1.0)
Expand Down Expand Up @@ -134,6 +151,7 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.1)
ffi (~> 1.0)
regexp_parser (1.7.0)
responders (3.0.0)
actionpack (>= 5.0)
railties (>= 5.0)
Expand All @@ -149,6 +167,14 @@ GEM
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-rails (4.0.1)
actionpack (>= 4.2)
activesupport (>= 4.2)
railties (>= 4.2)
rspec-core (~> 3.9)
rspec-expectations (~> 3.9)
rspec-mocks (~> 3.9)
rspec-support (~> 3.9)
rspec-support (3.9.2)
rubocop (0.79.0)
jaro_winkler (~> 1.5.1)
Expand All @@ -157,6 +183,10 @@ GEM
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.7)
rubocop-rails (2.5.2)
activesupport
rack (>= 1.1)
rubocop (>= 0.72.0)
ruby-progressbar (1.10.1)
ruby_dep (1.5.0)
sass (3.7.4)
Expand All @@ -170,6 +200,8 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
shoulda-matchers (4.3.0)
activesupport (>= 4.2.0)
spring (2.1.0)
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
Expand Down Expand Up @@ -202,6 +234,8 @@ GEM
websocket-driver (0.7.1)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.4)
xpath (3.2.0)
nokogiri (~> 1.8)

PLATFORMS
ruby
Expand All @@ -210,16 +244,22 @@ DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap (>= 1.1.0)
byebug
capybara (>= 2.15)
coffee-rails (~> 4.2)
database_cleaner (~> 1.8, >= 1.8.5)
devise
factory_bot_rails (~> 5.2)
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
puma (~> 3.11)
rails (~> 5.2.4)
rspec
rspec-rails (~> 4.0.0)
rubocop
rubocop-rails
sass-rails (~> 5.0)
shoulda-matchers
spring
spring-watcher-listen (~> 2.0.0)
turbolinks (~> 5)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/friendships_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class FriendshipsController < ApplicationController
before_action :set_status, only: [:update]
before_action :authenticate_user!

def index
@friends = current_user.friends
Expand Down
1 change: 0 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Application < Rails::Application
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.

# Don't generate system test files.
config.generators.system_tests = nil
end
Expand Down
4 changes: 1 addition & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_05_27_192244) do
ActiveRecord::Schema.define(version: 2020_05_26_212707) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -48,8 +48,6 @@
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "comments_count", default: 0
t.integer "likes_count", default: 0
t.index ["user_id"], name: "index_posts_on_user_id"
end

Expand Down
5 changes: 5 additions & 0 deletions spec/factories/comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :comment do

end
end
6 changes: 6 additions & 0 deletions spec/factories/posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :post do
content { 'MyString MyString MyString' }
user
end
end
8 changes: 8 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :user do
name { 'MyString' }
email { '[email protected]' }
password { '123456' }
password_confirmation { '123456' }
end
end
5 changes: 5 additions & 0 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Comment, type: :model do

end
17 changes: 17 additions & 0 deletions spec/models/post_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'rails_helper'

RSpec.describe Post, type: :model do
context 'Post Associations' do
it { should belong_to(:user) }
it { should have_many(:comments) }
it { should have_many(:likes) }
end
context 'Validation tests' do
let(:user) { create(:user) }
let(:post) { build(:post) }
it 'should save post' do
data = user.posts.build(post)
expect(data.save).to be(true)
end
end
end
17 changes: 17 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'rails_helper'

RSpec.describe User, type: :model do
describe 'User Associations' do
it { should have_many(:posts) }
it { should have_many(:comments) }
it { should have_many(:likes) }
end

describe 'Validation tests' do
let(:user) { build(:user) }

it 'should save user' do
expect(user.save).to eq(true)
end
end
end
72 changes: 72 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require './spec/support/factory_bot'
# 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 }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false

# 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

Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
Loading

0 comments on commit 28dd1a1

Please sign in to comment.