From 79fefa807e9c25bc7d1e0c5768c6a479164f4e77 Mon Sep 17 00:00:00 2001 From: AndriyAndriyovuch Date: Mon, 13 Nov 2023 14:30:07 +0200 Subject: [PATCH] fix test without monkey patching --- Gemfile | 2 +- Gemfile.lock | 4 ++-- app/models/app_config.rb | 2 -- spec/models/app_config_spec.rb | 11 ++++------- spec/requests/account/app_configs_spec.rb | 10 ++++------ 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/Gemfile b/Gemfile index bb39f2942..1da4a9b76 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby "3.2.2" -gem "mutex_m", "0.1.2" +gem "mutex_m", "0.2.0" gem "cancancan", "~> 3.3" gem "jbuilder", "~> 2.7" gem "pg", "~> 1.1" diff --git a/Gemfile.lock b/Gemfile.lock index 5bc53bce6..ab3f76a6a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -288,7 +288,7 @@ GEM multi_json (1.15.0) multi_xml (0.6.0) multipart-post (2.1.1) - mutex_m (0.1.2) + mutex_m (0.2.0) net-imap (0.4.4) date net-protocol @@ -620,7 +620,7 @@ DEPENDENCIES letter_opener listen (~> 3.3) mini_magick (>= 4.9.5) - mutex_m (= 0.1.2) + mutex_m (= 0.2.0) net-smtp omniauth (~> 1.9.1) omniauth-facebook diff --git a/app/models/app_config.rb b/app/models/app_config.rb index a649b7cf6..6cc02be35 100644 --- a/app/models/app_config.rb +++ b/app/models/app_config.rb @@ -12,8 +12,6 @@ class AppConfig < ApplicationRecord acts_as_singleton - public_class_method :find_by!, :all - def update_diapers_calculator(params) self.diapers_calculator = Calculators::DiapersService.product_attributes(params) save diff --git a/spec/models/app_config_spec.rb b/spec/models/app_config_spec.rb index 817843e26..0f53b69af 100644 --- a/spec/models/app_config_spec.rb +++ b/spec/models/app_config_spec.rb @@ -12,8 +12,6 @@ require "rails_helper" RSpec.describe AppConfig, type: :model do - fixtures :app_configs - context "is a singleton model" do it "should not allow using new method" do expect { AppConfig.new }.to raise_error NoMethodError @@ -24,15 +22,14 @@ end context "calls a service method" do - let(:app_config_instance) { app_configs(:initial_app_config) } + let(:app_config_instance) { AppConfig.instance } let(:diapers_calculator_params) { attributes_for(:diapers_calculator_params) } let(:updated_diapers_calculator) { attributes_for(:app_config, :updated) } it "updates the AppConfig instances diapers calculator" do - expect do - app_config_instance.update_diapers_calculator(diapers_calculator_params) - app_config_instance.reload - end.to change(app_config_instance, :diapers_calculator).to(updated_diapers_calculator[:diapers_calculator]) + app_config_instance.update_diapers_calculator(diapers_calculator_params) + + expect(app_config_instance[:diapers_calculator]).to eq(updated_diapers_calculator[:diapers_calculator]) end it "receives product attributes" do diff --git a/spec/requests/account/app_configs_spec.rb b/spec/requests/account/app_configs_spec.rb index 47600de48..da1ad971d 100644 --- a/spec/requests/account/app_configs_spec.rb +++ b/spec/requests/account/app_configs_spec.rb @@ -2,7 +2,6 @@ RSpec.describe Account::AppConfigsController, type: :request do include_context :authorize_admin - fixtures :app_configs describe "GET :edit" do it "is successful" do @@ -14,16 +13,15 @@ end describe "PATCH #update" do - let(:app_config_instance) { app_configs(:initial_app_config) } + let(:app_config_instance) { AppConfig.instance } let(:diapers_calculator_params) { attributes_for(:diapers_calculator_params) } let(:updated_diapers_calculator) { attributes_for(:app_config, :updated) } context "passed vaild params" do + before { patch account_app_config_path, params: diapers_calculator_params } + it "updates the AppConfig instances diapers calculator" do - expect do - patch account_app_config_path, params: diapers_calculator_params - app_config_instance.reload - end.to change(app_config_instance, :diapers_calculator).to(updated_diapers_calculator[:diapers_calculator]) + expect(app_config_instance[:diapers_calculator]).to eq(updated_diapers_calculator[:diapers_calculator]) end end end