diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 571d8ecc1..87a65219f 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -44,28 +44,6 @@ RSpec.describe CaseLog do end # TODO: replace these with validator specs and checks for method call here - context "with a reason for leaving last settled home validation" do - it "checks the reason for leaving must be don’t know if reason for leaving settled home (Q9a) is don’t know." do - expect { - described_class.create!(reason: "Don’t know", - underoccupation_benefitcap: "Yes - benefit cap", - owning_organisation:, - managing_organisation:) - }.to raise_error(ActiveRecord::RecordInvalid) - end - end - - context "with reason for leaving last settled home validation set to other" do - it "must not be provided if the main reason for leaving settled home is not other" do - expect { - described_class.create!(reason: "Repossession", - other_reason_for_leaving_last_settled_home: "the other reason provided", - owning_organisation:, - managing_organisation:) - }.to raise_error(ActiveRecord::RecordInvalid) - end - end - context "with armed forces injured validation" do it "must not be answered if tenant was not a regular or reserve in armed forces" do expect { @@ -690,6 +668,10 @@ RSpec.describe CaseLog do it "validates reasonable preference" do expect(validator).to receive(:validate_reasonable_preference) end + + it "validates reason for leaving last settled home" do + expect(validator).to receive(:validate_reason_for_leaving_last_settled_home) + end end describe "status" do diff --git a/spec/models/validations/household_validations_spec.rb b/spec/models/validations/household_validations_spec.rb index 12dbb7e80..c55679f37 100644 --- a/spec/models/validations/household_validations_spec.rb +++ b/spec/models/validations/household_validations_spec.rb @@ -167,17 +167,50 @@ RSpec.describe Validations::HouseholdValidations do end describe "reason for leaving last settled home validations" do + let(:field) { "validations.other_field_not_required" } + let(:main_field_label) { "reason" } + let(:other_field_label) { "other reason for leaving last settled home" } + let(:expected_error) { I18n.t(field, main_field_label:, other_field_label:) } + context "when reason is other" do + let(:field) { "validations.other_field_missing" } it "validates that a reason is provided" do record.reason = "Other" record.other_reason_for_leaving_last_settled_home = nil household_validator.validate_reason_for_leaving_last_settled_home(record) expect(record.errors["other_reason_for_leaving_last_settled_home"]) - .to include(match I18n.t( - "validations.other_field_missing", - main_field_label: "reason", - other_field_label: "other reason for leaving last settled home", - )) + .to include(match(expected_error)) + end + end + + context "when reason is not other" do + it "validates that other reason is not provided" do + record.reason = "Repossession" + record.other_reason_for_leaving_last_settled_home = "Some other reason" + household_validator.validate_reason_for_leaving_last_settled_home(record) + expect(record.errors["other_reason_for_leaving_last_settled_home"]) + .to include(match(expected_error)) + end + end + + context "when reason is not other" do + it "validates that other reason is not provided" do + record.reason = "Repossession" + record.other_reason_for_leaving_last_settled_home = "Some other reason" + household_validator.validate_reason_for_leaving_last_settled_home(record) + expect(record.errors["other_reason_for_leaving_last_settled_home"]) + .to include(match(expected_error)) + end + end + + context "when reason is don't know" do + let(:expected_error) { I18n.t("validations.household.underoccupation_benefitcap.dont_know_required") } + it "validates that under occupation benefit cap is also not known" do + record.reason = "Don’t know" + record.underoccupation_benefitcap = "Yes - benefit cap" + household_validator.validate_reason_for_leaving_last_settled_home(record) + expect(record.errors["underoccupation_benefitcap"]) + .to include(match(expected_error)) end end end