adzap / validates_timeliness

Date and time validation plugin for ActiveModel and Rails. Supports multiple ORMs and allows custom date/time formats.
MIT License
1.59k stars 227 forks source link

RSpec matcher? #115

Closed jmuheim closed 9 years ago

jmuheim commented 10 years ago

Is there something like this yet?

it { should validate_timeliness_of :birth_date }
guapolo commented 10 years ago

+1 for this feature :)

jehughes commented 10 years ago

+1 (please, pretty please)

virtualstaticvoid commented 10 years ago

Here is a naive RSpec matcher which simply checks whether the validator is present for the respective attribute. It does not check the options or whether there is an if or unless conditional applied to the validation.

RSpec::Matchers.define :validate_timeliness_of do |attribute|
  match do |model|
    model._validators[attribute.to_sym].detect do |validator|
      validator.class == ActiveModel::Validations::TimelinessValidator &&
        validator.attributes.include?(attribute)
    end
  end

  failure_message_for_should do |actual|
    "expect #{attribute} to validate timeliness of"
  end

  failure_message_for_should_not do |actual|
    "expect #{attribute} to not validate timeliness of"
  end
end
adzap commented 9 years ago

I did have such a matcher in earlier versions but I didn't find there is much value in such a matcher. But please create your own gem (e.g validates_timeliness-rpec) to explore it.