I have this Rails 6.1 model:
class BankAccount < ApplicationRecord
belongs_to :donor
before_validation -> {
puts #AccAuth: #{account_authorization}"
}
validates :account_authorization, acceptance: { accept: [true, 'true', '1'] }
end
The attribute "account_authorization" doesn't exist in the database, is not a SQL column, is a field from a JSON payload and must be one of the values in the array to be sure the bank answered successfully.
I'm trying to test in my Rspec file like:
let(:invalid_bank_account) do
Fabricate.build(:bank_account, account_authorization: nil, donor: Fabricate(:donor))
end
it 'is not valid record without a truty account_authorization value' do
puts "valid? #{invalid_bank_account.valid?}"
expect(invalid_bank_account).not_to be_valid
end
The result is:
### bank_account.rb >> AccAuth
>> nil
### bank_account_spec.rb >> valid?
>> true
Failure/Error: expect(invalid_bank_account).not_to be_valid
The validation is always successful even if there is no "account_authorization" value in the Model initializer. I'm validating a non-SQL attribute in the correct way?