Skip to content

Matcher validate_comparison_of is not working with ActiveRecord objects #1655

@cesarjr

Description

@cesarjr

Description

The matcher validate_comparison_of does not work when the attribute of the subject is also an ActiveRecord.

Reproduction Steps

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'
  gem 'shoulda-matchers'
  gem 'activerecord'
  gem 'sqlite3'
  gem 'rspec'
end

require 'active_record'
require 'shoulda-matchers'
require 'logger'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :accounts, force: true do |t|
    t.references :transfer_account, foreign_key: { to_table: :accounts }
  end
end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :active_record
    with.library :active_model
  end
end

RSpec.configure do |config|
  config.include Shoulda::Matchers::ActiveRecord
  config.include Shoulda::Matchers::ActiveModel
end

class Account < ActiveRecord::Base
  validates :transfer_account, comparison: { other_than: :itself }
end

RSpec.describe Account do
  describe '#transfer_account' do
    it { is_expected.to validate_comparison_of(:transfer_account).is_other_than(:itself) }
  end
end

RSpec::Core::Runner.run([$__FILE__])

Save the script above and run:

$ ruby test.rb

Expected behavior

The test should pass.

Actual behavior

image

Complement

I'm not sure if the problem is around of here.

System configuration

shoulda_matchers version: 6.4.0
rails version: 7.2.2
ruby version: 3.3.5p100

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions