From 350cd7e1f58a8114a409488b117b91aab016121c Mon Sep 17 00:00:00 2001 From: Rachael Booth Date: Thu, 7 Mar 2024 15:49:00 +0000 Subject: [PATCH] Don't depend on the rent_type for supported housing --- app/models/validations/tenancy_validations.rb | 1 - .../validations/tenancy_validations_spec.rb | 29 ++++++++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/models/validations/tenancy_validations.rb b/app/models/validations/tenancy_validations.rb index 29afc4b77..2950adba3 100644 --- a/app/models/validations/tenancy_validations.rb +++ b/app/models/validations/tenancy_validations.rb @@ -14,7 +14,6 @@ module Validations::TenancyValidations message = I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:) record.errors.add :needstype, message - record.errors.add :rent_type, message record.errors.add :tenancylength, :tenancylength_invalid, message: message record.errors.add :tenancy, message end diff --git a/spec/models/validations/tenancy_validations_spec.rb b/spec/models/validations/tenancy_validations_spec.rb index 282e94012..7b41e890b 100644 --- a/spec/models/validations/tenancy_validations_spec.rb +++ b/spec/models/validations/tenancy_validations_spec.rb @@ -8,7 +8,7 @@ RSpec.describe Validations::TenancyValidations do describe "tenancy length validations" do let(:record) { FactoryBot.create(:lettings_log, :setup_completed) } - shared_examples "adds expected errors based on the tenancy length" do |tenancy_type_case, min_tenancy_length| + shared_examples "adds expected errors based on the tenancy length" do |tenancy_type_case, error_fields, min_tenancy_length| context "and tenancy type is #{tenancy_type_case[:name]}" do let(:expected_error) { tenancy_type_case[:expected_error].call(min_tenancy_length) } @@ -17,24 +17,24 @@ RSpec.describe Validations::TenancyValidations do context "and tenancy length is less than #{min_tenancy_length}" do before { record.tenancylength = min_tenancy_length - 1 } - it "adds errors to #{tenancy_type_case[:error_fields].join(', ')}" do + it "adds errors to #{error_fields.join(', ')}" do validation.call(record) - tenancy_type_case[:error_fields].each do |field| + error_fields.each do |field| expect(record.errors[field]).to include(match(expected_error)) end - expect(record.errors.size).to be(tenancy_type_case[:error_fields].length) + expect(record.errors.size).to be(error_fields.length) end end context "and tenancy length is more than 99" do before { record.tenancylength = 100 } - it "adds errors to #{tenancy_type_case[:error_fields].join(', ')}" do + it "adds errors to #{error_fields.join(', ')}" do validation.call(record) - tenancy_type_case[:error_fields].each do |field| + error_fields.each do |field| expect(record.errors[field]).to include(match(expected_error)) end - expect(record.errors.size).to be(tenancy_type_case[:error_fields].length) + expect(record.errors.size).to be(error_fields.length) end end @@ -77,13 +77,11 @@ RSpec.describe Validations::TenancyValidations do name: "assured shorthold", code: 4, expected_error: ->(min_tenancy_length) { I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:) }, - error_fields: %w[needstype rent_type tenancylength tenancy], }, { name: "secure fixed term", code: 6, expected_error: ->(min_tenancy_length) { I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:) }, - error_fields: %w[needstype rent_type tenancylength tenancy], }, ] @@ -93,8 +91,9 @@ RSpec.describe Validations::TenancyValidations do context "when needs type is supported housing" do before { record.needstype = 2 } + error_fields = %w[needstype tenancylength tenancy] fixed_term_tenancy_type_cases.each do |tenancy_type_case| - include_examples "adds expected errors based on the tenancy length", tenancy_type_case, 1 + include_examples "adds expected errors based on the tenancy length", tenancy_type_case, error_fields, 1 end include_examples "does not add errors when tenancy type is not fixed term" @@ -123,8 +122,9 @@ RSpec.describe Validations::TenancyValidations do context "and rent type is affordable or social rent" do before { record.renttype = 1 } + error_fields = %w[needstype rent_type tenancylength tenancy] fixed_term_tenancy_type_cases.each do |tenancy_type_case| - include_examples "adds expected errors based on the tenancy length", tenancy_type_case, 2 + include_examples "adds expected errors based on the tenancy length", tenancy_type_case, error_fields, 2 end include_examples "does not add errors when tenancy type is not fixed term" @@ -168,8 +168,9 @@ RSpec.describe Validations::TenancyValidations do context "and rent type is intermediate rent" do before { record.renttype = 3 } + error_fields = %w[needstype rent_type tenancylength tenancy] fixed_term_tenancy_type_cases.each do |tenancy_type_case| - include_examples "adds expected errors based on the tenancy length", tenancy_type_case, 1 + include_examples "adds expected errors based on the tenancy length", tenancy_type_case, error_fields, 1 end include_examples "does not add errors when tenancy type is not fixed term" @@ -211,9 +212,9 @@ RSpec.describe Validations::TenancyValidations do name: "periodic", code: 8, expected_error: ->(min_tenancy_length) { I18n.t("validations.tenancy.length.invalid_periodic", min_tenancy_length:) }, - error_fields: %w[tenancylength tenancy], } - include_examples "adds expected errors based on the tenancy length", periodic_tenancy_case, 1 + error_fields = %w[tenancylength tenancy] + include_examples "adds expected errors based on the tenancy length", periodic_tenancy_case, error_fields, 1 context "when tenancy type is not periodic" do before do