简体   繁体   English

Rails validate_uniqueness_of rspec测试失败

[英]Rails validate_uniqueness_of rspec test fails

I trying to do testing at validation uniqueness with scope, but it fails 我试图在具有范围的验证唯一性上进行测试,但是失败了

Model 模型

 class EcrPortMapping < ActiveRecord::Base
      belongs_to :ecr
      validates :ecr_id, presence: true
      validates :ecr_id, uniqueness: {scope: :port_source, message: I18n.t('port_target_in_use')}
      validates :ecr_id, uniqueness: {scope: :port_target, message: I18n.t('port_source_in_use')}
   end

test 测试

    RSpec.describe EcrPortMapping, type: :model do
  describe 'validations' do

    it 'should validate uniqueness of ecr_id scoped to port_target & port_source' do
      should validate_uniqueness_of(:ecr_id).scoped_to(:port_target)
      should validate_uniqueness_of(:ecr_id).scoped_to(:port_source)
    end

error 错误

     Failure/Error: should validate_uniqueness_of(:ecr_id).scoped_to(:port_target)
   Expected validation to be scoped to [:port_target], but it was scoped to [:port_source].

It fails because it's scoped unique over both :port_target and :port_source . 它失败了,因为它在:port_target :port_source上都具有唯一作用域。 You can pass multiple scopes to scoped_to : 您可以将多个范围传递给scoped_to

it 'should validate uniqueness of ecr_id scoped to port_target & port_source' do
  should validate_uniqueness_of(:ecr_id).scoped_to(:port_target, :port_source)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM