igorkasyanchuk/active_storage_validations

Existing specs started failing after upgrade to 1.1.0. Probably an issue with the matcher

tagliala opened this issue · 6 comments

Hi,

some existing specs started failing after upgrade to 1.1.0.

This is an issue on both Rails 7.0 and 7.1 with different Ruby versions

This has been bisected to 927ffdf (PR #183)

Reproducible test case:

# frozen_string_literal: true

class User < ApplicationRecord
  has_one_attached :avatar

  # Remove `size: { greater_than: 0 }` to prevent failure or downgrade to 1.0.4
  validates :avatar, content_type: %w[image/jpeg], size: { greater_than: 0 }
end

Spec:

RSpec.describe User do
  it { is_expected.to validate_content_type_of(:avatar).allowing('image/jpeg') }
end

Results

1.0.4

User
  is expected to validate the content types allowed on attachment avatar

Finished in 0.20146 seconds (files took 1.9 seconds to load)
1 example, 0 failures

1.1.0

  1) User is expected to validate the content types allowed on attachment avatar
     Failure/Error: it { is_expected.to validate_content_type_of(:avatar).allowing('image/jpeg') }
     
       Expected avatar
       Accept content types: image/jpeg
        were rejected

Finished in 0.23732 seconds (files took 3.16 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:6 # User is expected to validate the content types allowed on attachment avatar

Repo: https://github.com/diowa/ruby3-rails7-bootstrap-heroku/tree/experimental/active-storage-validations


It appears to be a problem only in specs, the actual validation seems to work

@Mth0158 can you check this please

@igorkasyanchuk I'll look at it this afternoon and push a PR on this matter asap

Thanks for the fix and the prompt release

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

Hi,
That's what I was fearing, in my opinion, we are missing a bit of testing on that part of the gem so it's difficult to assess potential regressions. I am working on adding more test so that we do not have these issues again later.
I'll push a PR in the following days to definitely fix this, sorry for the inconvenience 😕

sorry for the inconvenience 😕

Absolutely no need for this! I'm sure you're doing your best and sharing it for free to everyone, so thanks a lot lot lot to you all!