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

should `validate_uniqueness_of` is expecting an invalid record with a different scoped value, which would be valid

Open chiperific opened this issue 1 year ago • 1 comments

Description

I extended shoulda_matchers for a concern that I wrote. This concern adds a uniqueness validation. The error message produced by shoulda_matchers makes me think shoulda_matchers isn't performing the right test.

Given this concern:

module Animal::Pet
  extend ActiveSupport::Concern

  ...
  included do
    # a name must be unique within a household
    validates_uniqueness_of :name, scope: :household_id, case_sensitive: false
  end
  ...
end

and this spec:

RSpec.configure do |config|
  config.include(Shoulda::Matchers::ActiveModel, type: :concern)
  config.include(Shoulda::Matchers::ActiveRecord, type: :concern)
end

RSpec.describe Animal::Pet, type: :concern do
 ...
 it { should validate_uniqueness_of(:name).scoped_to(:household_id).case_insensitive }
 ...
end

the spec fails with this message:

Expected Animal::Pet to validate that :name is case-insensitively unique within the scope of :household_id, but this could not be proved. After taking the given Animal::Pet, whose :name is ‹"Fluffy"›, and saving it as the existing record, then making a new Animal::Pet and setting its :name to ‹"Fluffy"› as well and its :household_id to a different value, ‹"b37b3f61-193c-4ff4-97db-5004e69e2c64"›, the matcher expected the new Animal::Pet to be invalid, but it was valid instead.

But the failure message makes it seem like the test is being performed wrong. Two pets can have the same name as long as they are in different households.

So I would expect a message like this:

After taking the given Animal::Pet, whose :name is ‹"Fluffy"›, and saving it as the existing record, then making a new Animal::Pet and setting its :name to ‹"Fluffy"› as well and its :household_id to ** the same ** value, ‹"b37b3f61-193c-4ff4-97db-5004e69e2c64"›, the matcher expected the new Animal::Pet to be invalid

System configuration

shoulda_matchers version: 6.4.0 rails version: 7.1 ruby version: 3.3.4

chiperific avatar Nov 20 '24 00:11 chiperific

Ok, so I found the issue: the records were not getting saved because of an around_create callback that didn't yield.

The spec now passes as expected.

However, the error message still seems to be wrong/misleading:

as well and its :household_id to a different value, ‹"b37b3f61-193c-4ff4-97db-5004e69e2c64"›

I would expect the message to read:

as well and its :household_id to the same value, ‹"b37b3f61-193c-4ff4-97db-5004e69e2c64"›

chiperific avatar Nov 20 '24 15:11 chiperific