spec/paperclip/validators_spec.rb in kt-paperclip-7.0.0 vs spec/paperclip/validators_spec.rb in kt-paperclip-7.0.1

- old
+ new

@@ -1,8 +1,13 @@ require "spec_helper" describe Paperclip::Validators do + # required to support a range of rubies + def error_attribute_names(error) + error.try(:attribute_names) || error.keys + end + context "using the helper" do before do rebuild_class Dummy.validates_attachment :avatar, presence: true, content_type: { content_type: "image/jpeg" }, size: { in: 0..10240 } end @@ -20,11 +25,13 @@ end it "prevents you from attaching a file that violates that validation" do Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } } dummy = Dummy.new(avatar: File.new(fixture_file("12k.png"))) - expect(dummy.errors.keys).to match_array [:avatar_content_type, :avatar, :avatar_file_size] + expect(error_attribute_names(dummy.errors)).to match_array( + %i[avatar_content_type avatar avatar_file_size] + ) assert_raises(RuntimeError) { dummy.valid? } end end context "using the helper with array of validations" do @@ -45,25 +52,31 @@ end it "prevents you from attaching a file that violates all of these validations" do Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } } dummy = Dummy.new(avatar: File.new(fixture_file("spaced file.png"))) - expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name] + expect(error_attribute_names(dummy.errors)).to match_array( + %i[avatar avatar_file_name] + ) assert_raises(RuntimeError) { dummy.valid? } end it "prevents you from attaching a file that violates only first of these validations" do Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } } dummy = Dummy.new(avatar: File.new(fixture_file("5k.png"))) - expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name] + expect(error_attribute_names(dummy.errors)).to match_array( + %i[avatar avatar_file_name] + ) assert_raises(RuntimeError) { dummy.valid? } end it "prevents you from attaching a file that violates only second of these validations" do Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } } dummy = Dummy.new(avatar: File.new(fixture_file("spaced file.jpg"))) - expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name] + expect(error_attribute_names(dummy.errors)).to match_array( + %i[avatar avatar_file_name] + ) assert_raises(RuntimeError) { dummy.valid? } end it "allows you to attach a file that does not violate these validations" do dummy = Dummy.new(avatar: File.new(fixture_file("rotated.jpg"))) @@ -86,20 +99,22 @@ def title_present? true end end dummy = Dummy.new(avatar: File.new(fixture_file("12k.png"))) - expect(dummy.errors.keys).to match_array [:avatar_content_type, :avatar, :avatar_file_size] + expect(error_attribute_names(dummy.errors)).to match_array( + %i[avatar_content_type avatar avatar_file_size] + ) end it "does not validate attachment if title is not present" do Dummy.class_eval do def title_present? false end end dummy = Dummy.new(avatar: File.new(fixture_file("12k.png"))) - assert_equal [], dummy.errors.keys + assert_equal [], error_attribute_names(dummy.errors) end end context "with no other validations on the Dummy#avatar attachment" do before do