From a9be6d3b40fc46d44a52b7fc6b66b68d8d6443a5 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Tue, 17 May 2022 23:42:51 -0300 Subject: [PATCH] ref: extract support configs --- spec/common_helper.rb | 16 +++++++++++++ spec/config/rate_table_spec.rb | 2 -- spec/config/rating_table_spec.rb | 2 -- spec/config/validations_spec.rb | 2 -- spec/models/extension/after_create_spec.rb | 2 -- spec/models/extension/order_by_rating_spec.rb | 1 - spec/models/extension/rate_for_spec.rb | 2 -- spec/models/extension/rate_spec.rb | 2 -- spec/models/extension/rated_question_spec.rb | 2 -- spec/models/extension/rated_records_spec.rb | 1 - spec/models/extension/rated_spec.rb | 1 - spec/models/extension/rates_records_spec.rb | 1 - spec/models/extension/rates_spec.rb | 1 - spec/models/extension/rating_records_spec.rb | 1 - spec/models/extension/rating_spec.rb | 1 - spec/models/extension/rating_warm_up_spec.rb | 2 -- spec/models/extension/unscoped_rating_spec.rb | 2 -- spec/models/extension/where_spec.rb | 2 -- spec/models/rate/create_spec.rb | 2 -- spec/models/rate/rate_for_spec.rb | 2 -- spec/models/rate_spec.rb | 22 ++++++++--------- spec/models/rating/averager_data_spec.rb | 1 - spec/models/rating/data_spec.rb | 1 - spec/models/rating/update_rating_spec.rb | 1 - spec/models/rating/values_data_spec.rb | 1 - spec/models/rating_spec.rb | 22 ++++++++--------- spec/rails_helper.rb | 16 ------------- spec/support/common.rb | 14 +---------- spec/support/models.rb | 3 +++ .../shared_context/with_database_records.rb | 24 +++++++++---------- 30 files changed, 54 insertions(+), 98 deletions(-) create mode 100644 spec/common_helper.rb delete mode 100644 spec/rails_helper.rb create mode 100644 spec/support/models.rb diff --git a/spec/common_helper.rb b/spec/common_helper.rb new file mode 100644 index 0000000..347e720 --- /dev/null +++ b/spec/common_helper.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +ENV['RAILS_ENV'] ||= 'test' + +require 'support/coverage' + +require 'active_record/railtie' +require 'debug' +require 'rating' + +require 'support/common' +require 'support/mysql' +require 'support/database_cleaner' +require 'support/factory_bot' +require 'support/migrate' +require 'support/models' diff --git a/spec/config/rate_table_spec.rb b/spec/config/rate_table_spec.rb index a050a2f..3f1b9f5 100644 --- a/spec/config/rate_table_spec.rb +++ b/spec/config/rate_table_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Config, '.rate_table' do if ENV['CONFIG_ENABLED'] != 'true' context 'when rating.yml does not exist' do diff --git a/spec/config/rating_table_spec.rb b/spec/config/rating_table_spec.rb index c9fa426..0627185 100644 --- a/spec/config/rating_table_spec.rb +++ b/spec/config/rating_table_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Config, '.rating_table' do if ENV['CONFIG_ENABLED'] != 'true' context 'when rating.yml does not exist' do diff --git a/spec/config/validations_spec.rb b/spec/config/validations_spec.rb index 1336744..72a5b28 100644 --- a/spec/config/validations_spec.rb +++ b/spec/config/validations_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Config, '.validations' do if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] != 'true' context 'when rating.yml does not exist' do diff --git a/spec/models/extension/after_create_spec.rb b/spec/models/extension/after_create_spec.rb index cf14e90..4f801c5 100644 --- a/spec/models/extension/after_create_spec.rb +++ b/spec/models/extension/after_create_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Extension, 'after_create' do context 'when record is author' do let!(:record) { build :author } diff --git a/spec/models/extension/order_by_rating_spec.rb b/spec/models/extension/order_by_rating_spec.rb index 46449a0..a39f018 100644 --- a/spec/models/extension/order_by_rating_spec.rb +++ b/spec/models/extension/order_by_rating_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, ':order_by_rating' do diff --git a/spec/models/extension/rate_for_spec.rb b/spec/models/extension/rate_for_spec.rb index 577fa40..7204337 100644 --- a/spec/models/extension/rate_for_spec.rb +++ b/spec/models/extension/rate_for_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Extension, ':rate_for' do let!(:author) { create :author } let!(:article) { create :article } diff --git a/spec/models/extension/rate_spec.rb b/spec/models/extension/rate_spec.rb index 6ee12c1..3c37fb7 100644 --- a/spec/models/extension/rate_spec.rb +++ b/spec/models/extension/rate_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Extension, ':rate' do let!(:author) { create :author } let!(:article) { create :article } diff --git a/spec/models/extension/rated_question_spec.rb b/spec/models/extension/rated_question_spec.rb index f03505d..d5ad62e 100644 --- a/spec/models/extension/rated_question_spec.rb +++ b/spec/models/extension/rated_question_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Extension, ':rated?' do let!(:author) { create :author } let!(:resource) { create :article } diff --git a/spec/models/extension/rated_records_spec.rb b/spec/models/extension/rated_records_spec.rb index 8883431..1fda090 100644 --- a/spec/models/extension/rated_records_spec.rb +++ b/spec/models/extension/rated_records_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, '.rated_records' do diff --git a/spec/models/extension/rated_spec.rb b/spec/models/extension/rated_spec.rb index 44739ca..5ccfd98 100644 --- a/spec/models/extension/rated_spec.rb +++ b/spec/models/extension/rated_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, ':rated' do diff --git a/spec/models/extension/rates_records_spec.rb b/spec/models/extension/rates_records_spec.rb index 90c6e6b..b5c0aa2 100644 --- a/spec/models/extension/rates_records_spec.rb +++ b/spec/models/extension/rates_records_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, '.rates_records' do diff --git a/spec/models/extension/rates_spec.rb b/spec/models/extension/rates_spec.rb index 65ff6c5..cdb3eb3 100644 --- a/spec/models/extension/rates_spec.rb +++ b/spec/models/extension/rates_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, ':rates' do diff --git a/spec/models/extension/rating_records_spec.rb b/spec/models/extension/rating_records_spec.rb index 78f9fbc..a88be30 100644 --- a/spec/models/extension/rating_records_spec.rb +++ b/spec/models/extension/rating_records_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, '.rating' do diff --git a/spec/models/extension/rating_spec.rb b/spec/models/extension/rating_spec.rb index a0a0cf2..aaaf59d 100644 --- a/spec/models/extension/rating_spec.rb +++ b/spec/models/extension/rating_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' require 'support/shared_context/with_database_records' RSpec.describe Rating::Extension, '.rating' do diff --git a/spec/models/extension/rating_warm_up_spec.rb b/spec/models/extension/rating_warm_up_spec.rb index 3040000..3466744 100644 --- a/spec/models/extension/rating_warm_up_spec.rb +++ b/spec/models/extension/rating_warm_up_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Extension, '.rating_warm_up' do context 'when scoping is nil' do context 'when update is made' do diff --git a/spec/models/extension/unscoped_rating_spec.rb b/spec/models/extension/unscoped_rating_spec.rb index 29a8f08..3f068e3 100644 --- a/spec/models/extension/unscoped_rating_spec.rb +++ b/spec/models/extension/unscoped_rating_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Extension, 'unscoped_rating' do let!(:author_1) { create :author } let!(:author_2) { create :author } diff --git a/spec/models/extension/where_spec.rb b/spec/models/extension/where_spec.rb index ed56bde..aba4f66 100644 --- a/spec/models/extension/where_spec.rb +++ b/spec/models/extension/where_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Extension, 'unscoped_rating' do let!(:author_1) { create :author } let!(:author_2) { create :author } diff --git a/spec/models/rate/create_spec.rb b/spec/models/rate/create_spec.rb index fd63c4f..64beecc 100644 --- a/spec/models/rate/create_spec.rb +++ b/spec/models/rate/create_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Rate, ':create' do let!(:author) { create :author } let!(:article) { create :article } diff --git a/spec/models/rate/rate_for_spec.rb b/spec/models/rate/rate_for_spec.rb index ede6c50..02b0375 100644 --- a/spec/models/rate/rate_for_spec.rb +++ b/spec/models/rate/rate_for_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' - RSpec.describe Rating::Rate, ':rate_for' do let!(:author) { create :author } let!(:article) { create :article } diff --git a/spec/models/rate_spec.rb b/spec/models/rate_spec.rb index 95231d6..ed91f53 100644 --- a/spec/models/rate_spec.rb +++ b/spec/models/rate_spec.rb @@ -1,27 +1,27 @@ # frozen_string_literal: true -require 'rails_helper' +require 'support/shoulda' RSpec.describe Rating::Rate do - let!(:object) { build :rating_rate } + let!(:object) { build(:rating_rate) } - it { expect(object).to be_valid } + it { expect(object.valid?).to be(true) } - it { is_expected.to belong_to :author } - it { is_expected.to belong_to :resource } - it { is_expected.to belong_to :scopeable } + it { is_expected.to belong_to(:author) } + it { is_expected.to belong_to(:resource) } + it { is_expected.to belong_to(:scopeable) } - it { is_expected.to validate_presence_of :author } - it { is_expected.to validate_presence_of :resource } - it { is_expected.to validate_presence_of :value } + it { is_expected.to validate_presence_of(:author) } + it { is_expected.to validate_presence_of(:resource) } + it { is_expected.to validate_presence_of(:value) } it do - expect(subject).to validate_numericality_of(:value).is_less_than_or_equal_to(100).is_less_than_or_equal_to 100 + expect(subject).to validate_numericality_of(:value).is_less_than_or_equal_to(100).is_less_than_or_equal_to(100) end it do scopes = %i[author_type resource_id resource_type scopeable_id scopeable_type] - scopes += %i[scope_1 scope_2] if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true' + scopes += %i[scope_1 scope_2] if ENV.fetch('CONFIG_ENABLED_WITH_EXTRA_SCOPES', 'false') == 'true' expect(object).to validate_uniqueness_of(:author_id).scoped_to(scopes).case_insensitive end diff --git a/spec/models/rating/averager_data_spec.rb b/spec/models/rating/averager_data_spec.rb index e95e325..a6a95e5 100644 --- a/spec/models/rating/averager_data_spec.rb +++ b/spec/models/rating/averager_data_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' require 'support/shared_context/with_database_records' RSpec.describe Rating::Rating, ':averager_data' do diff --git a/spec/models/rating/data_spec.rb b/spec/models/rating/data_spec.rb index b2674ff..f3726c8 100644 --- a/spec/models/rating/data_spec.rb +++ b/spec/models/rating/data_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' require 'support/shared_context/with_database_records' RSpec.describe Rating::Rating, ':data' do diff --git a/spec/models/rating/update_rating_spec.rb b/spec/models/rating/update_rating_spec.rb index 1dc8992..8c3bb25 100644 --- a/spec/models/rating/update_rating_spec.rb +++ b/spec/models/rating/update_rating_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' require 'support/shared_context/with_database_records' RSpec.describe Rating::Rating, ':update_rating' do diff --git a/spec/models/rating/values_data_spec.rb b/spec/models/rating/values_data_spec.rb index ac4dee1..12a824e 100644 --- a/spec/models/rating/values_data_spec.rb +++ b/spec/models/rating/values_data_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'rails_helper' require 'support/shared_context/with_database_records' RSpec.describe Rating::Rating, ':values_data' do diff --git a/spec/models/rating_spec.rb b/spec/models/rating_spec.rb index eeaf788..5a1de9c 100644 --- a/spec/models/rating_spec.rb +++ b/spec/models/rating_spec.rb @@ -1,20 +1,20 @@ # frozen_string_literal: true -require 'rails_helper' +require 'support/shoulda' -RSpec.describe Rating::Rating do - let!(:object) { build :rating_rating } +RSpec.describe Rating::Rating, type: :model do + let!(:object) { build(:rating_rating) } - it { expect(object).to be_valid } + it { expect(object.valid?).to be(true) } - it { is_expected.to belong_to :resource } - it { is_expected.to belong_to :scopeable } + it { is_expected.to belong_to(:resource) } + it { is_expected.to belong_to(:scopeable) } - it { is_expected.to validate_presence_of :average } - it { is_expected.to validate_presence_of :estimate } - it { is_expected.to validate_presence_of :resource } - it { is_expected.to validate_presence_of :sum } - it { is_expected.to validate_presence_of :total } + it { is_expected.to validate_presence_of(:average) } + it { is_expected.to validate_presence_of(:estimate) } + it { is_expected.to validate_presence_of(:resource) } + it { is_expected.to validate_presence_of(:sum) } + it { is_expected.to validate_presence_of(:total) } it do expect(object).to validate_uniqueness_of(:resource_id) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb deleted file mode 100644 index 9a291f0..0000000 --- a/spec/rails_helper.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -ENV['RAILS_ENV'] ||= 'test' - -require 'active_record/railtie' -require 'mysql2' -require 'rating' - -client = Mysql2::Client.new(host: '127.0.0.1', username: :root) - -client.query 'DROP DATABASE IF EXISTS rating_test;' -client.query 'CREATE DATABASE IF NOT EXISTS rating_test;' - -ActiveRecord::Base.establish_connection adapter: :mysql2, database: :rating_test, username: :root, host: '127.0.0.1' - -Dir[File.expand_path('support/**/*.rb', __dir__)].each { |file| require file } diff --git a/spec/support/common.rb b/spec/support/common.rb index d601c1b..3d62209 100644 --- a/spec/support/common.rb +++ b/spec/support/common.rb @@ -3,20 +3,8 @@ require 'rspec/rails' RSpec.configure do |config| - config.filter_run_when_matching :focus - config.disable_monkey_patching! config.infer_spec_type_from_file_location! - config.expect_with :rspec do |expectations| - expectations.include_chain_clauses_in_custom_matcher_descriptions = true - end - - config.mock_with :rspec do |mocks| - mocks.verify_partial_doubles = true - end - - config.infer_base_class_for_anonymous_controllers = false - config.order = :random - config.profile_examples = true + config.order = :random end diff --git a/spec/support/models.rb b/spec/support/models.rb new file mode 100644 index 0000000..f0b8c56 --- /dev/null +++ b/spec/support/models.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +Dir[File.expand_path('models/*.rb', __dir__)].each { |file| require file } diff --git a/spec/support/shared_context/with_database_records.rb b/spec/support/shared_context/with_database_records.rb index 087212a..c7df02a 100644 --- a/spec/support/shared_context/with_database_records.rb +++ b/spec/support/shared_context/with_database_records.rb @@ -1,20 +1,20 @@ # frozen_string_literal: true RSpec.shared_context 'with_database_records' do - let!(:category) { create :category } + let!(:category) { create(:category) } - let!(:author_1) { create :author } - let!(:author_2) { create :author } + let!(:author_1) { create(:author) } + let!(:author_2) { create(:author) } - let!(:article_1) { create :article } - let!(:article_2) { create :article } - let!(:article_3) { create :article } + let!(:article_1) { create(:article) } + let!(:article_2) { create(:article) } + let!(:article_3) { create(:article) } - let!(:rate_1) { create :rating_rate, author: author_1, resource: article_1, value: 100 } - let!(:rate_2) { create :rating_rate, author: author_1, resource: article_2, value: 11 } - let!(:rate_3) { create :rating_rate, author: author_1, resource: article_3, value: 10 } - let!(:rate_4) { create :rating_rate, author: author_2, resource: article_1, value: 1 } + let!(:rate_1) { create(:rating_rate, author: author_1, resource: article_1, value: 100) } + let!(:rate_2) { create(:rating_rate, author: author_1, resource: article_2, value: 11) } + let!(:rate_3) { create(:rating_rate, author: author_1, resource: article_3, value: 10) } + let!(:rate_4) { create(:rating_rate, author: author_2, resource: article_1, value: 1) } - let!(:rate_5) { create :rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1 } - let!(:rate_6) { create :rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2 } + let!(:rate_5) { create(:rating_rate, author: author_1, resource: article_1, scopeable: category, value: 1) } + let!(:rate_6) { create(:rating_rate, author: author_2, resource: article_1, scopeable: category, value: 2) } end