最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rspec - Rails acceptance validation always correct - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Feb 6 at 0:26 aarkerioaarkerio 2,3542 gold badges22 silver badges38 bronze badges 1
  • 1 That's not going to work -- the acceptance validation is not really meant for working with a json attribute in a column. Much simpler to write a custom validation. – dbugger Commented Feb 6 at 0:53
Add a comment  | 

1 Answer 1

Reset to default 4

If attribute is nil it is ignored:

...This check is performed only if terms_of_service is not nil.

https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_acceptance_of

You have to specifically disallow nil:

class Account < ApplicationRecord
  validates :account_authorization, acceptance: {allow_nil: false}
end
>> Account.new(account_authorization: nil).valid?
=> false
>> Account.new(account_authorization: "1").valid?
=> true

Note, that this will force you to supply account_authorization value every time you're making an update.

发布评论

评论列表(0)

  1. 暂无评论