Browse Source
* validate sales log head count between 0 and 4 * enable ability to focus rspecspull/1018/head v0.2.21
Phil Lee
2 years ago
committed by
GitHub
4 changed files with 72 additions and 8 deletions
@ -0,0 +1,9 @@ |
|||||||
|
module Validations::Sales::HouseholdValidations |
||||||
|
def validate_number_of_other_people_living_in_the_property(record) |
||||||
|
return if record.hholdcount.blank? |
||||||
|
|
||||||
|
unless record.hholdcount >= 0 && record.hholdcount <= 4 |
||||||
|
record.errors.add :hholdcount, I18n.t("validations.numeric.valid", field: "Number of other people living in the property", min: 0, max: 4) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,49 @@ |
|||||||
|
require "rails_helper" |
||||||
|
|
||||||
|
RSpec.describe Validations::Sales::HouseholdValidations do |
||||||
|
subject(:household_validator) { validator_class.new } |
||||||
|
|
||||||
|
let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } } |
||||||
|
|
||||||
|
describe "#validate_number_of_other_people_living_in_the_property" do |
||||||
|
context "when within permitted bounds" do |
||||||
|
let(:record) { FactoryBot.build(:sales_log, hholdcount: 2) } |
||||||
|
|
||||||
|
it "does not add an error" do |
||||||
|
household_validator.validate_number_of_other_people_living_in_the_property(record) |
||||||
|
|
||||||
|
expect(record.errors).not_to be_present |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when blank" do |
||||||
|
let(:record) { FactoryBot.build(:sales_log, hholdcount: nil) } |
||||||
|
|
||||||
|
it "does not add an error" do |
||||||
|
household_validator.validate_number_of_other_people_living_in_the_property(record) |
||||||
|
|
||||||
|
expect(record.errors).not_to be_present |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when below lower bound" do |
||||||
|
let(:record) { FactoryBot.build(:sales_log, hholdcount: -1) } |
||||||
|
|
||||||
|
it "adds an error" do |
||||||
|
household_validator.validate_number_of_other_people_living_in_the_property(record) |
||||||
|
|
||||||
|
expect(record.errors).to be_present |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when higher than upper bound" do |
||||||
|
let(:record) { FactoryBot.build(:sales_log, hholdcount: 5) } |
||||||
|
|
||||||
|
it "adds an error" do |
||||||
|
household_validator.validate_number_of_other_people_living_in_the_property(record) |
||||||
|
|
||||||
|
expect(record.errors).to be_present |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue