shoulda-matchers icon indicating copy to clipboard operation
shoulda-matchers copied to clipboard

allow_value not handling aliased attributes

Open rubendinho opened this issue 3 years ago • 0 comments

I am running into problems using allow_value with an alias attribute.

This is my model, which has a column called custom_data, aliased as metadata.

class Widget < ApplicationRecord
  alias_attribute :metadata, :custom_data
  validates :metadata, presence: true, allow_nil: true
  ...
end

In my tests, using the allow_value is not working as expected with the original column name custom_data.

expect(Widget.new).not_to allow_value("").for(:metadata) passes as expected. 🔴 expect(Widget.new).not_to allow_value("").for(:custom_data) fails with:

RSpec::Expectations::ExpectationNotMetError: After setting :custom_data to ‹""›, the matcher expected the Widget to be invalid, but it was valid instead.

This seems like a bug because the record Widget.new(metadata: "").invalid? and Widget.new(custom_data: "").invalid? will both evaluate to true.

Other matchers likevalidate_presence_of, are respecting the alias, as expected: ✅ it { expect(Widget.new).to validate_presence_of(:metadata).allow_nil }it { expect(Widget.new).to validate_presence_of(:custom_data).allow_nil }

I am running Rails 6.1, with v5.0.0 of the gem (same issue with v4.5.1)

rubendinho avatar Sep 17 '21 00:09 rubendinho