Browse Source

write tests for new validations

validations-copy
Arthur Campbell 2 years ago
parent
commit
206872939e
  1. 58
      spec/models/validations/shared_validations_spec.rb

58
spec/models/validations/shared_validations_spec.rb

@ -114,6 +114,64 @@ RSpec.describe Validations::SharedValidations do
end end
end end
describe "validating level of accuracy or rounding for numeric questions" do
context "when validating a question with a step of 1" do
it "adds an error if input is a decimal" do
sales_log.income1 = 30_000.5
shared_validator.validate_numeric_step(sales_log)
expect(sales_log.errors[:income1]).to include I18n.t("validations.numeric.whole_number", field: "Buyer 1’s gross annual income")
end
it "does not add an error if input is an integer" do
sales_log.income1 = 30_000
shared_validator.validate_numeric_step(sales_log)
expect(sales_log.errors).to be_empty
end
end
context "when validating a question with a step of 10" do
it "adds an error if input is not a multiple of ten" do
sales_log.savings = 30_005
shared_validator.validate_numeric_step(sales_log)
expect(sales_log.errors[:savings]).to include I18n.t("validations.numeric.nearest_ten", field: "Buyer’s total savings (to nearest £10) before any deposit paid")
end
it "does not add an error if input is a multiple of ten" do
sales_log.savings = 30_000
shared_validator.validate_numeric_step(sales_log)
expect(sales_log.errors).to be_empty
end
end
context "when validating a question with a step of 0.01" do
it "adds an error if input has more than 2 decimal places" do
sales_log.deposit = 630.7418
shared_validator.validate_numeric_step(sales_log)
expect(sales_log.errors[:deposit]).to include I18n.t("validations.numeric.nearest_penny", field: "Cash deposit")
end
it "does not add an error if input has 2 or fewer decimal places" do
sales_log.deposit = 630.74
shared_validator.validate_numeric_step(sales_log)
expect(sales_log.errors).to be_empty
end
end
end
describe "validating the format of inputs in numeric questions" do
it "adds an error if number is input in exponent notation" do
sales_log.age2 = "1e1"
shared_validator.validate_numeric_normal_format(sales_log)
expect(sales_log.errors[:age2]).to include I18n.t("validations.numeric.normal_format")
end
it "does not add an error if number is input in standard notation" do
sales_log.age2 = "11"
shared_validator.validate_numeric_normal_format(sales_log)
expect(sales_log.errors).to be_empty
end
end
describe "radio options validations" do describe "radio options validations" do
it "allows only possible values" do it "allows only possible values" do
lettings_log.needstype = 1 lettings_log.needstype = 1

Loading…
Cancel
Save