From 519a7d2ee6e40a856a5d692e35f3065ab54c9da3 Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Fri, 21 Sep 2018 17:29:27 +0800 Subject: [PATCH] Add datetime and integer array validator matchers --- .../matchers/date_time_validator_matchers.rb | 30 +++++++++++++++++++ .../integer_array_validator_matchers.rb | 30 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 spec/support/matchers/date_time_validator_matchers.rb create mode 100644 spec/support/matchers/integer_array_validator_matchers.rb diff --git a/spec/support/matchers/date_time_validator_matchers.rb b/spec/support/matchers/date_time_validator_matchers.rb new file mode 100644 index 0000000000..f0fdaf678c --- /dev/null +++ b/spec/support/matchers/date_time_validator_matchers.rb @@ -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 diff --git a/spec/support/matchers/integer_array_validator_matchers.rb b/spec/support/matchers/integer_array_validator_matchers.rb new file mode 100644 index 0000000000..44cff638aa --- /dev/null +++ b/spec/support/matchers/integer_array_validator_matchers.rb @@ -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