Validator custom error message causes test matcher error
Mth0158 opened this issue · 5 comments
From @Alagaesia93 in #201
Hi there!
I got into the same issue today and 1.1.1 does not fix it if you have a custom message.
In your example
validates :avatar, content_type: %w[image/jpeg], size: { greater_than: 0, message: "whatever foobar" }
The spec fails
My example is with less_than, but I think it's the same
class Shop < ApplicationRecord
has_one_attached :logo
validates :logo, size: { less_than: 10.megabytes }, content_type: ['image/png', 'image/jpg', 'image/jpeg'] # works
validates :logo, size: { less_than: 10.megabytes, message: "must be less than 10 MB" }, content_type: ['image/png', 'image/jpg', 'image/jpeg'] # fails
end
describe Shop do
it { is_expected.to validate_size_of(:logo).less_than(10.megabytes) }
end
Originally posted by @Alagaesia93 in #201 (comment)
I can add more context. We have a test which looks like this:
test 'should validate attachment format' do
project = create(:project)
project.image.attach(io: file_fixture("hello.csv"), filename: "hello.csv")
project.validate
assert project.errors.of_kind? :image, :content_type_invalid
end
The assertion fails now. When inspecting the project.errors
, it looks like this:
#<ActiveModel::Errors [#<ActiveModel::Error attribute=image, type=Das Bild muss im JPG oder PNG-Format sein, options={:message=>"Das Bild muss im JPG oder PNG-Format sein", :authorized_types=>"X-PNG, PNG, JPG, JPEG, PJPEG", :content_type=>"text/csv"}>]>
Prior to v1.1, it looked like:
#<ActiveModel::Errors [#<ActiveModel::Error attribute=image, type=content_type_invalid, options={:authorized_types=>"X-PNG, PNG, JPG, JPEG, PJPEG", :message=>"Das Bild muss im JPG oder PNG-Format sein", :content_type=>"text/csv"}>]>
Think the key difference is that type
now includes the translated message, instead of the actual error type.
Thanks a lot @andyundso! I am currently refactoring the matcher tests of the gem so we do not get these regressions.
This issue should be corrected soon (1 or 2 weeks), I'll keep you update once it's merged 📦
Hi @Mth0158
I updated to v1.1.3 today and still see the behaviour as I reported in #203 (comment). Is this the expected behaviour now?
Hi @andyundso
I thought it was corrected, but this edge case is not handled properly. I think it's the same as #234 because there are 2 size validators
on the same attribute.
I'll work a on a fix in the coming days, if it's the same issue as #234 I'll close this issue again, let me know
Hi @andyundso,
The fix has been merged, it'll be available in the next release ;)