From 66fdc9cb095e637da0c0b1bf9190a229b6170152 Mon Sep 17 00:00:00 2001 From: McEileen Date: Fri, 15 Nov 2024 13:05:48 +0000 Subject: [PATCH 1/2] improve error message for nil client share --- app/models/partners/served_area.rb | 2 +- spec/models/partners/served_area_spec.rb | 30 +++++++++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/models/partners/served_area.rb b/app/models/partners/served_area.rb index 5fa2ea92a5..00bb758c26 100644 --- a/app/models/partners/served_area.rb +++ b/app/models/partners/served_area.rb @@ -16,6 +16,6 @@ class ServedArea < ApplicationRecord belongs_to :partner_profile, class_name: "Partners::Profile" belongs_to :county validates :client_share, numericality: {only_integer: true} - validates :client_share, inclusion: {in: 1..100} + validates :client_share, inclusion: {in: 1..100, message: "Client share must be between 1 and 100 inclusive"} end end diff --git a/spec/models/partners/served_area_spec.rb b/spec/models/partners/served_area_spec.rb index 949d99ed80..df604027df 100644 --- a/spec/models/partners/served_area_spec.rb +++ b/spec/models/partners/served_area_spec.rb @@ -11,19 +11,27 @@ # RSpec.describe Partners::ServedArea, type: :model do - it { should belong_to(:partner_profile) } - it { should belong_to(:county) } + describe "validations" do + let(:served_area_nil_client_share) { build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: nil) } + it { should belong_to(:partner_profile) } + it { should belong_to(:county) } - it "must only allow integer client shares" do - expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 50)).to be_valid - expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 50.5)).not_to be_valid - end + it "displays a user friendly error message for nil client shares" do + expect(served_area_nil_client_share).not_to be_valid + expect(served_area_nil_client_share.errors.messages[:client_share]).to match_array(["Client share must be between 1 and 100 inclusive", "is not a number"]) + end + + it "must only allow integer client shares" do + expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 50)).to be_valid + expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 50.5)).not_to be_valid + end - it "must only allow valid client share values" do - expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 0)).not_to be_valid - expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 101)).not_to be_valid - expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 1)).to be_valid - expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 100)).to be_valid + it "must only allow valid client share values" do + expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 0)).not_to be_valid + expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 101)).not_to be_valid + expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 1)).to be_valid + expect(build(:partners_served_area, partner_profile: create(:partner_profile), county: create(:county), client_share: 100)).to be_valid + end end describe "versioning" do From beec1c83b44718dec0d28be50d8067fdc113d904 Mon Sep 17 00:00:00 2001 From: McEileen Date: Fri, 15 Nov 2024 13:30:54 +0000 Subject: [PATCH 2/2] modify partner profile update service spec to reflect new error message --- spec/services/partner_profile_update_service_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/services/partner_profile_update_service_spec.rb b/spec/services/partner_profile_update_service_spec.rb index 25b2446bc5..97108d73ba 100644 --- a/spec/services/partner_profile_update_service_spec.rb +++ b/spec/services/partner_profile_update_service_spec.rb @@ -88,7 +88,7 @@ expect(profile.served_areas.size).to eq(2) result = PartnerProfileUpdateService.new(profile.partner, partner_params, incorrect_attributes_missing_client_share).call expect(result.success?).to eq(false) - expect(result.error.to_s).to include("Validation failed: Served areas client share is not a number, Served areas client share is not included in the list") + expect(result.error.to_s).to include("Validation failed: Served areas client share is not a number, Served areas client share Client share must be between 1 and 100 inclusive") profile.reload expect(profile.served_areas.size).to eq(2) end