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:
Expected behavior
The test should pass.
Actual behavior
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
Description
The matcher
validate_comparison_ofdoes not work when the attribute of the subject is also an ActiveRecord.Reproduction Steps
Save the script above and run:
Expected behavior
The test should pass.
Actual behavior
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