spec/lib/validation_spec.rb in flexirest-1.3.32 vs spec/lib/validation_spec.rb in flexirest-1.3.33

- old
+ new

@@ -1,11 +1,11 @@ require 'spec_helper' describe "Flexirest::Validation" do class SimpleValidationExample < OpenStruct include Flexirest::Validation - validates :first_name, presence: true + validates :first_name, presence: true, message: "Sorry, something went wrong" validates :middle_name, length: { minimum: 2, maximum: 30 }, allow_nil: true validates :last_name, existence: true validates :nick_name, length: { minimum: 2, maximum: 30 } validates :alias, length: { minimum: 2, maximum: 30 }, allow_nil: false validates :password, length: { within: 6..12 } @@ -32,10 +32,17 @@ a.first_name = nil a.valid? expect(a._errors[:first_name].size).to eq(1) end + it "should return a custom error if specified and a validation fails" do + a = SimpleValidationExample.new + a.first_name = nil + a.valid? + expect(a._errors[:first_name][0]).to eq("Sorry, something went wrong") + end + it "should be invalid if a required value is present but blank" do a = SimpleValidationExample.new a.first_name = "" a.valid? expect(a._errors[:first_name].size).to eq(1) @@ -58,11 +65,11 @@ it "should be valid if a required value is present" do a = SimpleValidationExample.new a.first_name = "John" a.valid? expect(a._errors[:first_name]).to be_empty - end + end end context "when validating existence" do it "should be invalid if a required value isn't present" do a = SimpleValidationExample.new @@ -81,11 +88,11 @@ it "should be valid if a required value is present" do a = SimpleValidationExample.new a.last_name = "John" a.valid? expect(a._errors[:last_name]).to be_empty - end + end end context "when validating length" do it "should be invalid if a length within value is outside the range" do a = SimpleValidationExample.new(password:"12345") @@ -114,11 +121,11 @@ it "should be invalid if a length is above the maximum" do a = SimpleValidationExample.new(post_code:"123456789") a.valid? expect(a._errors[:post_code].size).to eq(1) end - + it "should be valid if a length is nil and allow_nil option is true" do a = SimpleValidationExample.new a.valid? expect(a._errors[:middle_name]).to be_empty end @@ -153,17 +160,17 @@ it "should be able to validate that a numeric field is above or equal to a minimum" do a = SimpleValidationExample.new(salary:100_000) a.valid? expect(a._errors[:salary].size).to be > 0 - end + end it "should be valid that a numeric field is above or equal to a minimum" do a = SimpleValidationExample.new(salary:30_000) a.valid? expect(a._errors[:salary].size).to eq(0) - end + end it "should be valid if a value is nil and allow_nil option is true" do a = SimpleValidationExample.new a.valid? expect(a._errors[:golf_score]).to be_empty @@ -203,33 +210,33 @@ it "should be able to validate that a numeric field is above or equal to a minimum" do a = SimpleValidationExample.new(age: 70) a.valid? expect(a._errors[:age].size).to be > 0 - end + end it "should be valid that a numeric field is above or equal to a minimum" do a = SimpleValidationExample.new(age: 30) a.valid? expect(a._errors[:age].size).to eq(0) - end + end end end context "when validating inclusion" do it "should be invalid if the value is not contained in the list" do a = SimpleValidationExample.new(suffix: "Baz") a.valid? - expect(a._errors[:suffix].size).to be > 0 + expect(a._errors[:suffix].size).to be > 0 end it "should be valid if the value is contained in the list" do a = SimpleValidationExample.new(suffix: "Dr.") a.valid? - expect(a._errors[:suffix].size).to eq(0) + expect(a._errors[:suffix].size).to eq(0) end - + it "should be valid if the value is nil and allow_nil option is true" do a = SimpleValidationExample.new a.valid? expect(a._errors[:favorite_authors]).to be_empty end @@ -268,10 +275,10 @@ end end a = ValidationExample2.new(first_name:"Johnny") a.valid? expect(a._errors[:first_name]).to be_empty - end + end end describe "#full_error_messages" do it "should return an array of strings that combines the attribute name and the error message" do a = SimpleValidationExample.new(age:"Bob", suffix: "Baz")