diff --git a/test/dummy/app/models/dimension/validator/check.rb b/test/dummy/app/models/dimension/validator/check.rb index 00217e0..9862bda 100644 --- a/test/dummy/app/models/dimension/validator/check.rb +++ b/test/dummy/app/models/dimension/validator/check.rb @@ -42,6 +42,13 @@ class Dimension::Validator::Check < ApplicationRecord validates :"#{bound}_proc", dimension: { "#{bound}": -> (record) { 500..500 } } end + # Integration tests + has_one_attached :width_height_exact + validates :width_height_exact, dimension: { width: 600, height: 600 } + has_one_attached :width_height_in + validates :width_height_in, dimension: { width: { in: 550..750 }, height: { in: 550..750 } } + + # Edge cases has_one_attached :with_invalid_media_file validates :with_invalid_media_file, dimension: { width: 150, height: 150 } end diff --git a/test/dummy/public/image_500x700.png b/test/dummy/public/image_500x700.png new file mode 100644 index 0000000..262b962 Binary files /dev/null and b/test/dummy/public/image_500x700.png differ diff --git a/test/dummy/public/image_600x600.png b/test/dummy/public/image_600x600.png new file mode 100644 index 0000000..a292833 Binary files /dev/null and b/test/dummy/public/image_600x600.png differ diff --git a/test/support/files.rb b/test/support/files.rb index f85fdb0..8a74108 100644 --- a/test/support/files.rb +++ b/test/support/files.rb @@ -24,6 +24,22 @@ def image_500x500_file } end +def image_600x600_file + { + io: File.open(Rails.root.join('public', 'image_600x600.png')), + filename: 'image_600x600_file.png', + content_type: 'image/png' + } +end + +def image_500x700_file + { + io: File.open(Rails.root.join('public', 'image_500x700.png')), + filename: 'image_500x700_file.png', + content_type: 'image/png' + } +end + def image_700x500_file { io: File.open(Rails.root.join('public', 'image_700x500.png')), diff --git a/test/validators/aspect_ratio_validator_test.rb b/test/validators/aspect_ratio_validator_test.rb index 82b75a6..269d480 100644 --- a/test/validators/aspect_ratio_validator_test.rb +++ b/test/validators/aspect_ratio_validator_test.rb @@ -151,7 +151,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("aspect_ratio_not_#{named_aspect_ratio}", error_options: error_options) } + it { is_expected_to_include_error_message("aspect_ratio_not_#{named_aspect_ratio}", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -183,7 +183,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("aspect_ratio_not_x_y", error_options: error_options) } + it { is_expected_to_include_error_message("aspect_ratio_not_x_y", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -218,7 +218,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message('aspect_ratio_invalid', error_options: error_options) } + it { is_expected_to_include_error_message('aspect_ratio_invalid', error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -237,7 +237,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("media_metadata_missing", error_options: error_options) } + it { is_expected_to_include_error_message("media_metadata_missing", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end diff --git a/test/validators/attached_validator_test.rb b/test/validators/attached_validator_test.rb index 02dbad6..38b446a 100644 --- a/test/validators/attached_validator_test.rb +++ b/test/validators/attached_validator_test.rb @@ -34,7 +34,7 @@ subject { model } it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("blank", error_options: {}) } + it { is_expected_to_include_error_message("blank", error_options: {}) } end describe 'when provided with a file that is marked for destruction' do @@ -42,7 +42,7 @@ subject { model.has_to_be_attached.attach(image_1920x1080_file) and model.has_to_be_attached.mark_for_destruction and model } it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("blank", error_options: {}) } + it { is_expected_to_include_error_message("blank", error_options: {}) } end end diff --git a/test/validators/content_type_validator_test.rb b/test/validators/content_type_validator_test.rb index 45a57a0..751a5cc 100644 --- a/test/validators/content_type_validator_test.rb +++ b/test/validators/content_type_validator_test.rb @@ -130,7 +130,7 @@ # end # it { is_expected_not_to_be_valid } - # it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) } + # it { is_expected_to_include_error_message("content_type_invalid", error_options: error_options) } # it { is_expected_to_have_error_options(error_options) } # end @@ -153,7 +153,7 @@ # end # it { is_expected_not_to_be_valid } - # it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) } + # it { is_expected_to_include_error_message("content_type_invalid", error_options: error_options) } # it { is_expected_to_have_error_options(error_options) } # end @@ -203,7 +203,7 @@ # end # it { is_expected_not_to_be_valid } - # it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) } + # it { is_expected_to_include_error_message("content_type_invalid", error_options: error_options) } # it { is_expected_to_have_error_options(error_options) } # end # end @@ -244,7 +244,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) } + it { is_expected_to_include_error_message("content_type_invalid", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -289,7 +289,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) } + it { is_expected_to_include_error_message("content_type_invalid", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -353,7 +353,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) } + it { is_expected_to_include_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) } it { is_expected_to_have_error_options(error_options, validator: :content_type) } end @@ -375,7 +375,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) } + it { is_expected_to_include_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) } it { is_expected_to_have_error_options(error_options, validator: :content_type) } end @@ -397,7 +397,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) } + it { is_expected_to_include_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) } it { is_expected_to_have_error_options(error_options, validator: :content_type) } end @@ -457,7 +457,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) } + it { is_expected_to_include_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) } it { is_expected_to_have_error_options(error_options, validator: :content_type) } end end diff --git a/test/validators/dimension_validator_test.rb b/test/validators/dimension_validator_test.rb index bbde3b6..550c1fa 100644 --- a/test/validators/dimension_validator_test.rb +++ b/test/validators/dimension_validator_test.rb @@ -102,7 +102,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("dimension_#{dimension}_not_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_#{dimension}_not_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -124,7 +124,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("dimension_#{dimension}_not_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_#{dimension}_not_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -148,7 +148,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("dimension_#{dimension}_not_greater_than_or_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_#{dimension}_not_greater_than_or_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -196,7 +196,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("dimension_#{dimension}_not_less_than_or_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_#{dimension}_not_less_than_or_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -220,7 +220,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("dimension_#{dimension}_not_greater_than_or_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_#{dimension}_not_greater_than_or_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -242,7 +242,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("dimension_#{dimension}_not_less_than_or_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_#{dimension}_not_less_than_or_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -267,7 +267,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("dimension_#{dimension}_not_included_in", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_#{dimension}_not_included_in", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -290,7 +290,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("dimension_#{dimension}_not_included_in", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_#{dimension}_not_included_in", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -319,7 +319,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("dimension_min_not_included_in", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_min_not_included_in", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -371,7 +371,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("dimension_max_not_included_in", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_max_not_included_in", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -379,6 +379,135 @@ end end + describe "Integration tests" do + describe ":width exact + :height exact" do + let(:attribute) { :width_height_exact } + + describe "when provided with an image that has the same width and height as specified in the model validations" do + subject { model.public_send(attribute).attach(image_600x600_file) and model } + + it { is_expected_to_be_valid } + end + + describe "when provided with an image that has a different width than specified in the model validations" do + subject { model.public_send(attribute).attach(image_800x600_file) and model } + + let(:error_options) do + { + width: 600, + height: 600, + length: 600, + filename: 'image_800x600_file.png' + } + end + + it { is_expected_not_to_be_valid } + it { is_expected_to_include_error_message("dimension_width_not_equal_to", error_options: error_options) } + it { is_expected_to_have_error_options(error_options) } + end + + describe "when provided with an image that has a different height than specified in the model validations" do + subject { model.public_send(attribute).attach(image_600x800_file) and model } + + let(:error_options) do + { + width: 600, + height: 600, + length: 600, + filename: 'image_600x800_file.png' + } + end + + it { is_expected_not_to_be_valid } + it { is_expected_to_include_error_message("dimension_height_not_equal_to", error_options: error_options) } + it { is_expected_to_have_error_options(error_options) } + end + + describe "when provided with an image that has a different height and a different width than specified in the model validations" do + subject { model.public_send(attribute).attach(image_1200x900_file) and model } + + let(:error_options) do + { + width: 600, + height: 600, + length: 600, + filename: 'image_1200x900_file.png' + } + end + + it { is_expected_not_to_be_valid } + it { is_expected_to_include_error_message("dimension_width_not_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_height_not_equal_to", error_options: error_options) } + it { is_expected_to_have_error_options(error_options) } + end + end + + describe ":width in + :height in" do + let(:attribute) { :width_height_in } + + describe "when provided with an image that has a valid width and height as specified in the model validations" do + subject { model.public_send(attribute).attach(image_600x600_file) and model } + + it { is_expected_to_be_valid } + end + + describe "when provided with an image that has lower width than the min specified in the model validations" do + subject { model.public_send(attribute).attach(image_500x700_file) and model } + + let(:error_options) do + { + width: { in: 550..750}, + height: { in: 550..750 }, + min: 550, + max: 750, + filename: 'image_500x700_file.png' + } + end + + it { is_expected_not_to_be_valid } + it { is_expected_to_include_error_message("dimension_width_not_included_in", error_options: error_options) } + it { is_expected_to_have_error_options(error_options) } + end + + describe "when provided with an image that has lower height than the min specified in the model validations" do + subject { model.public_send(attribute).attach(image_700x500_file) and model } + + let(:error_options) do + { + width: { in: 550..750}, + height: { in: 550..750 }, + min: 550, + max: 750, + filename: 'image_700x500_file.png' + } + end + + it { is_expected_not_to_be_valid } + it { is_expected_to_include_error_message("dimension_height_not_included_in", error_options: error_options) } + it { is_expected_to_have_error_options(error_options) } + end + + describe "when provided with an image that has lower width and height than the mins specified in the model validations" do + subject { model.public_send(attribute).attach(image_500x500_file) and model } + + let(:error_options) do + { + width: { in: 550..750}, + height: { in: 550..750 }, + min: 550, + max: 750, + filename: 'image_500x500_file.png' + } + end + + it { is_expected_not_to_be_valid } + it { is_expected_to_include_error_message("dimension_width_not_included_in", error_options: error_options) } + it { is_expected_to_include_error_message("dimension_height_not_included_in", error_options: error_options) } + it { is_expected_to_have_error_options(error_options) } + end + end + end + describe "Edge cases" do describe "when the passed file is not a valid media" do subject { model.public_send(attribute).attach(empty_io_file) and model } @@ -391,7 +520,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("media_metadata_missing", error_options: error_options) } + it { is_expected_to_include_error_message("media_metadata_missing", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end diff --git a/test/validators/duration_validator_test.rb b/test/validators/duration_validator_test.rb index 45774fd..a616777 100644 --- a/test/validators/duration_validator_test.rb +++ b/test/validators/duration_validator_test.rb @@ -42,7 +42,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("duration_not_less_than", error_options: error_options) } + it { is_expected_to_include_error_message("duration_not_less_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -59,7 +59,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("duration_not_less_than", error_options: error_options) } + it { is_expected_to_include_error_message("duration_not_less_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -96,7 +96,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("duration_not_less_than_or_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("duration_not_less_than_or_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -121,7 +121,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("duration_not_greater_than", error_options: error_options) } + it { is_expected_to_include_error_message("duration_not_greater_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -138,7 +138,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("duration_not_greater_than", error_options: error_options) } + it { is_expected_to_include_error_message("duration_not_greater_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -169,7 +169,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("duration_not_greater_than_or_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("duration_not_greater_than_or_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -206,7 +206,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("duration_not_between", error_options: error_options) } + it { is_expected_to_include_error_message("duration_not_between", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -241,7 +241,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("duration_not_between", error_options: error_options) } + it { is_expected_to_include_error_message("duration_not_between", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end diff --git a/test/validators/integration/integration_test.rb b/test/validators/integration/integration_test.rb index 30c1305..cc64a59 100644 --- a/test/validators/integration/integration_test.rb +++ b/test/validators/integration/integration_test.rb @@ -23,7 +23,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_not_processable", error_options: error_options, validator: :processable_file) } + it { is_expected_to_include_error_message("file_not_processable", error_options: error_options, validator: :processable_file) } it { is_expected_to_have_error_options(error_options, validator: :processable_file) } end end @@ -51,7 +51,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_size_not_less_than", error_options: error_options, validator: :size) } + it { is_expected_to_include_error_message("file_size_not_less_than", error_options: error_options, validator: :size) } it { is_expected_to_have_error_options(error_options, validator: :size) } end end @@ -69,7 +69,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_size_not_less_than", error_options: error_options, validator: :size) } + it { is_expected_to_include_error_message("file_size_not_less_than", error_options: error_options, validator: :size) } it { is_expected_to_have_error_options(error_options, validator: :size) } end end diff --git a/test/validators/limit_validator_test.rb b/test/validators/limit_validator_test.rb index 5b9f290..a2e8dbb 100644 --- a/test/validators/limit_validator_test.rb +++ b/test/validators/limit_validator_test.rb @@ -75,7 +75,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("limit_min_not_reached", error_options: error_options) } + it { is_expected_to_include_error_message("limit_min_not_reached", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -112,7 +112,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("limit_max_exceeded", error_options: error_options) } + it { is_expected_to_include_error_message("limit_max_exceeded", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -151,7 +151,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("limit_out_of_range", error_options: error_options) } + it { is_expected_to_include_error_message("limit_out_of_range", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -168,7 +168,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("limit_out_of_range", error_options: error_options) } + it { is_expected_to_include_error_message("limit_out_of_range", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end diff --git a/test/validators/processable_file_validator_test.rb b/test/validators/processable_file_validator_test.rb index 9945284..b7142b1 100644 --- a/test/validators/processable_file_validator_test.rb +++ b/test/validators/processable_file_validator_test.rb @@ -44,7 +44,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_not_processable", error_options: error_options) } + it { is_expected_to_include_error_message("file_not_processable", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end diff --git a/test/validators/shared_examples/works_with_message_option.rb b/test/validators/shared_examples/works_with_message_option.rb index 5496a2b..a5f5a80 100644 --- a/test/validators/shared_examples/works_with_message_option.rb +++ b/test/validators/shared_examples/works_with_message_option.rb @@ -43,7 +43,7 @@ module WorksWithMessageOption before { subject.with_message.attach(file_not_matching_requirements) } it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("Custom message", error_options: error_options) } + it { is_expected_to_include_error_message("Custom message", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end diff --git a/test/validators/size_validator_test.rb b/test/validators/size_validator_test.rb index b30523a..ef386e2 100644 --- a/test/validators/size_validator_test.rb +++ b/test/validators/size_validator_test.rb @@ -41,7 +41,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_size_not_less_than", error_options: error_options) } + it { is_expected_to_include_error_message("file_size_not_less_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -58,7 +58,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_size_not_less_than", error_options: error_options) } + it { is_expected_to_include_error_message("file_size_not_less_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -95,7 +95,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_size_not_less_than_or_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("file_size_not_less_than_or_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -120,7 +120,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_size_not_greater_than", error_options: error_options) } + it { is_expected_to_include_error_message("file_size_not_greater_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -137,7 +137,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_size_not_greater_than", error_options: error_options) } + it { is_expected_to_include_error_message("file_size_not_greater_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -168,7 +168,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_size_not_greater_than_or_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("file_size_not_greater_than_or_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -205,7 +205,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_size_not_between", error_options: error_options) } + it { is_expected_to_include_error_message("file_size_not_between", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -240,7 +240,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("file_size_not_between", error_options: error_options) } + it { is_expected_to_include_error_message("file_size_not_between", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end diff --git a/test/validators/support/validator_helpers.rb b/test/validators/support/validator_helpers.rb index 55e5ac0..c990514 100644 --- a/test/validators/support/validator_helpers.rb +++ b/test/validators/support/validator_helpers.rb @@ -28,20 +28,20 @@ def is_expected_to_have_error_options(error_options, **kwargs) ) end - def is_expected_to_have_error_message(message_key, **kwargs) + def is_expected_to_include_error_message(message_key, **kwargs) subject.valid?(kwargs[:context]) - validator_error_message = - subject.errors.find do |error| + validator_error_messages = + subject.errors.select do |error| error.options[:validator_type] == kwargs[:validator] || validator_sym - end&.message + end.map(&:message) message = kwargs[:error_options][:custom_message] || I18n.t("errors.messages.#{message_key}", **kwargs[:error_options]) - assert_equal( + assert_includes( + validator_error_messages, message, - validator_error_message, - "Expected error message to be '#{message.inspect}'\nbut got '#{validator_error_message.inspect}'" + "Expected error messages to include '#{message.inspect}'\nbut got #{validator_error_messages.inspect}" ) end diff --git a/test/validators/total_size_validator_test.rb b/test/validators/total_size_validator_test.rb index e337e00..8968e47 100644 --- a/test/validators/total_size_validator_test.rb +++ b/test/validators/total_size_validator_test.rb @@ -50,7 +50,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("total_file_size_not_less_than", error_options: error_options) } + it { is_expected_to_include_error_message("total_file_size_not_less_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -66,7 +66,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("total_file_size_not_less_than", error_options: error_options) } + it { is_expected_to_include_error_message("total_file_size_not_less_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -102,7 +102,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("total_file_size_not_less_than_or_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("total_file_size_not_less_than_or_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end @@ -126,7 +126,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("total_file_size_not_greater_than", error_options: error_options) } + it { is_expected_to_include_error_message("total_file_size_not_greater_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -142,7 +142,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("total_file_size_not_greater_than", error_options: error_options) } + it { is_expected_to_include_error_message("total_file_size_not_greater_than", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -172,7 +172,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("total_file_size_not_greater_than_or_equal_to", error_options: error_options) } + it { is_expected_to_include_error_message("total_file_size_not_greater_than_or_equal_to", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -208,7 +208,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("total_file_size_not_between", error_options: error_options) } + it { is_expected_to_include_error_message("total_file_size_not_between", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end @@ -242,7 +242,7 @@ end it { is_expected_not_to_be_valid } - it { is_expected_to_have_error_message("total_file_size_not_between", error_options: error_options) } + it { is_expected_to_include_error_message("total_file_size_not_between", error_options: error_options) } it { is_expected_to_have_error_options(error_options) } end end