Skip to content

Can't test the validation with allow_blank: true #140

@v-tsvid

Description

@v-tsvid

Hi! I have the Order model with :completed_date attribute. And I would like to validate :completed_date in the following way

class Order < ActiveRecord::Base
  #...
  validates_date :completed_date, allow_blank: true
  #...
end

But my spec for this validation doesn't work

it "is invalid when completed_date is invalid" do
  order.completed_date = "incorrect date"
  expect{ order.valid? }.to change{ order.errors.messages[:completed_date] }
end

I believe it's because any incorrect date value sets to nil and nil is valid value since I have allow_blank: true in my validation

When I remove allow_blank: true from my validation, the spec above works perfectly but I really need to permit blank :completed_date for my orders.

So I wonder if I do something wrong or if there is any way to test such validation?

P.S. I have tried all of the following constructions and all of them didn't work
validates_date :completed_date, allow_blank: true
validates :completed_date, timeliness: { type: :date }, allow_blank: true
validates :completed_date, timeliness: { type: :date, allow_blank: true }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions