From 28dd1a1feac55dcd5c799c4e31588f6c7c1ac060 Mon Sep 17 00:00:00 2001 From: kwizl Date: Thu, 28 May 2020 00:44:03 +0300 Subject: [PATCH] Add testing frameworks for unit and integration test --- Gemfile | 9 ++ Gemfile.lock | 40 +++++++++ app/controllers/friendships_controller.rb | 1 + config/application.rb | 1 - db/schema.rb | 4 +- spec/factories/comments.rb | 5 ++ spec/factories/posts.rb | 6 ++ spec/factories/users.rb | 8 ++ spec/models/comment_spec.rb | 5 ++ spec/models/post_spec.rb | 17 ++++ spec/models/user_spec.rb | 17 ++++ spec/rails_helper.rb | 72 +++++++++++++++ spec/spec_helper.rb | 103 +++++++++++----------- spec/support/factory_bot.rb | 3 + 14 files changed, 235 insertions(+), 56 deletions(-) create mode 100644 spec/factories/comments.rb create mode 100644 spec/factories/posts.rb create mode 100644 spec/factories/users.rb create mode 100644 spec/models/comment_spec.rb create mode 100644 spec/models/post_spec.rb create mode 100644 spec/models/user_spec.rb create mode 100644 spec/rails_helper.rb create mode 100644 spec/support/factory_bot.rb diff --git a/Gemfile b/Gemfile index e7abdf6ad7..a26682025e 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 2270c3d22e..54dfc3ea50 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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) diff --git a/app/controllers/friendships_controller.rb b/app/controllers/friendships_controller.rb index 1241be4367..67e8d1ef59 100644 --- a/app/controllers/friendships_controller.rb +++ b/app/controllers/friendships_controller.rb @@ -1,5 +1,6 @@ class FriendshipsController < ApplicationController before_action :set_status, only: [:update] + before_action :authenticate_user! def index @friends = current_user.friends diff --git a/config/application.rb b/config/application.rb index 1cbf7597fa..e3ccb2d18f 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 101b370d94..44da97239b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 diff --git a/spec/factories/comments.rb b/spec/factories/comments.rb new file mode 100644 index 0000000000..0415df5c22 --- /dev/null +++ b/spec/factories/comments.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :comment do + + end +end diff --git a/spec/factories/posts.rb b/spec/factories/posts.rb new file mode 100644 index 0000000000..a69d5d1e0f --- /dev/null +++ b/spec/factories/posts.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :post do + content { 'MyString MyString MyString' } + user + end +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 0000000000..5e7857dcbe --- /dev/null +++ b/spec/factories/users.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :user do + name { 'MyString' } + email { 'example@gmail.com' } + password { '123456' } + password_confirmation { '123456' } + end +end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb new file mode 100644 index 0000000000..917d87a340 --- /dev/null +++ b/spec/models/comment_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Comment, type: :model do + +end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb new file mode 100644 index 0000000000..cf864e5443 --- /dev/null +++ b/spec/models/post_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000000..67dc28c9e5 --- /dev/null +++ b/spec/models/user_spec.rb @@ -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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000000..40169880d1 --- /dev/null +++ b/spec/rails_helper.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a403ac5360..7591dc75d8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,5 @@ -# This file was generated by the `rspec --init` command. Conventionally, all +require 'capybara/rspec' +# This file was generated by the `rails generate rspec:install` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause # this file to always be loaded, without a need to explicitly require it in any @@ -44,55 +45,53 @@ # triggering implicit auto-inclusion in groups with matching metadata. config.shared_context_metadata_behavior = :apply_to_host_groups - # The settings below are suggested to provide a good initial experience - # with RSpec, but feel free to customize to your heart's content. - # # This allows you to limit a spec run to individual examples or groups - # # you care about by tagging them with `:focus` metadata. When nothing - # # is tagged with `:focus`, all examples get run. RSpec also provides - # # aliases for `it`, `describe`, and `context` that include `:focus` - # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. - # config.filter_run_when_matching :focus - # - # # Allows RSpec to persist some state between runs in order to support - # # the `--only-failures` and `--next-failure` CLI options. We recommend - # # you configure your source control system to ignore this file. - # config.example_status_persistence_file_path = "spec/examples.txt" - # - # # Limits the available syntax to the non-monkey patched syntax that is - # # recommended. For more details, see: - # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ - # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ - # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode - # config.disable_monkey_patching! - # - # # This setting enables warnings. It's recommended, but in some cases may - # # be too noisy due to issues in dependencies. - # config.warnings = true - # - # # Many RSpec users commonly either run the entire suite or an individual - # # file, and it's useful to allow more verbose output when running an - # # individual spec file. - # if config.files_to_run.one? - # # Use the documentation formatter for detailed output, - # # unless a formatter has already been configured - # # (e.g. via a command-line flag). - # config.default_formatter = "doc" - # end - # - # # Print the 10 slowest examples and example groups at the - # # end of the spec run, to help surface which specs are running - # # particularly slow. - # config.profile_examples = 10 - # - # # Run specs in random order to surface order dependencies. If you find an - # # order dependency and want to debug it, you can fix the order by providing - # # the seed, which is printed after each run. - # # --seed 1234 - # config.order = :random - # - # # Seed global randomization in this process using the `--seed` CLI option. - # # Setting this allows you to use `--seed` to deterministically reproduce - # # test failures related to randomization by passing the same `--seed` value - # # as the one that triggered the failure. - # Kernel.srand config.seed +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end end diff --git a/spec/support/factory_bot.rb b/spec/support/factory_bot.rb new file mode 100644 index 0000000000..195f20aad6 --- /dev/null +++ b/spec/support/factory_bot.rb @@ -0,0 +1,3 @@ +RSpec.configure do |config| + config.include FactoryBot::Syntax::Methods +end \ No newline at end of file