Ruby on rails rails模型测试验证\u唯一性范围

Ruby on rails rails模型测试验证\u唯一性范围,ruby-on-rails,unit-testing,rspec,Ruby On Rails,Unit Testing,Rspec,我试图对范围验证进行rspec模型测试,但不明白为什么会出现错误 我的模型 class EcrPortMapping < ActiveRecord::Base belongs_to :ecr validates :ecr_id,presence: true validates :ecr_id, :uniqueness => {:scope => [:port_source, :port_target]} validates :ecr_id, :uniquenes

我试图对范围验证进行rspec模型测试,但不明白为什么会出现错误

我的模型

class EcrPortMapping < ActiveRecord::Base
  belongs_to :ecr
  validates :ecr_id,presence: true
  validates :ecr_id, :uniqueness => {:scope => [:port_source, :port_target]}
  validates :ecr_id, :uniqueness => {:scope => :port_source}
  validates :ecr_id, :uniqueness => {:scope => :port_target}
end
我有错误

Failure/Error: should validate_uniqueness_of(:ecr_id).scoped_to([:port_source, :port_target])
   Did not expect errors to include "already exists" when ecr_id is set to 4,
   got errors:
   * "already exists" (attribute: ecr_id, value: 4) (with different value of port_source)
我需要这种类型的验证


我哪里出错了?

更改端口在ecr范围内应该是唯一的

validate :port_source, : uniqueness => { :scope => :ecr_id }
validate :port_target, : uniqueness => { :scope => :ecr_id }
validate :port_source, : uniqueness => { :scope => :ecr_id }
validate :port_target, : uniqueness => { :scope => :ecr_id }