mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-09 23:06:06 +00:00
Add datetime and integer array validator matchers
This commit is contained in:
30
spec/support/matchers/date_time_validator_matchers.rb
Normal file
30
spec/support/matchers/date_time_validator_matchers.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
# RSpec matcher for DateTimeValidator
|
||||
#
|
||||
# Usage:
|
||||
#
|
||||
# describe Post do
|
||||
# it { should validate_date_time_format_of(:start_at) }
|
||||
# end
|
||||
RSpec::Matchers.define :validate_date_time_format_of do |attribute|
|
||||
match do |instance|
|
||||
@instance, @attribute = instance, attribute
|
||||
|
||||
invalid_format_message = I18n.t("validators.date_time_string_validator.invalid_format_error")
|
||||
|
||||
allow(instance).to receive(attribute) { "Invalid Format" }
|
||||
instance.valid?
|
||||
(instance.errors[attribute] || []).include?(invalid_format_message)
|
||||
end
|
||||
|
||||
description do
|
||||
"validates :#{@attribute} has datetime format"
|
||||
end
|
||||
|
||||
failure_message do
|
||||
"expected #{@instance} to validate format of :#{@attribute} is datetime"
|
||||
end
|
||||
|
||||
failure_message_when_negated do
|
||||
"expected #{@instance} not to validate format of :#{@attribute} is datetime"
|
||||
end
|
||||
end
|
||||
30
spec/support/matchers/integer_array_validator_matchers.rb
Normal file
30
spec/support/matchers/integer_array_validator_matchers.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
# RSpec matcher for IntegerArrayValidator
|
||||
#
|
||||
# Usage:
|
||||
#
|
||||
# describe Post do
|
||||
# it { should validate_integer_array(:related_post_ids) }
|
||||
# end
|
||||
RSpec::Matchers.define :validate_integer_array do |attribute|
|
||||
match do |instance|
|
||||
@instance, @attribute = instance, attribute
|
||||
|
||||
invalid_format_message = I18n.t("validators.integer_array_validator.invalid_element_error")
|
||||
|
||||
allow(instance).to receive(attribute) { [1, "2", "Not Integer", 3] }
|
||||
instance.valid?
|
||||
(instance.errors[attribute] || []).include?(invalid_format_message)
|
||||
end
|
||||
|
||||
description do
|
||||
"validates :#{@attribute} is integer array"
|
||||
end
|
||||
|
||||
failure_message do
|
||||
"expected #{@instance} to validate :#{@attribute} is integer array"
|
||||
end
|
||||
|
||||
failure_message_when_negated do
|
||||
"expected #{@instance} not to validate :#{@attribute} is integer array"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user