[英]Rspec validate uniqueness with scope
I have this in my Line model我的 Line 模型中有这个
validates :home_team, :uniqueness => { :scope => [:visiting_team, :event_datetime],
:message => "** DOUBLE EVENT **" }
I have this in my spec我的规格中有这个
describe Line do
it { should validate_uniqueness_of(:home_team).scoped_to(:visiting_team, :event_datetime) }
I get this error...我得到这个错误...
Failures:失败:
1) Line
Failure/Error:
it { should validate_uniqueness_of(:home_team).scoped_to(:visiting_team, :event_datetime) }
Did not expect errors to include "has already been taken" when home_team is set to "arbitrary_string", got error:
# ./spec/models/line_spec.rb:7:in `block (2 levels) in <top (required)>'
Any ideas why this is failing?任何想法为什么这会失败?
I think you need to do this make it pass我认为你需要这样做让它通过
it { should validate_uniqueness_of(:home_team).scoped_to(:visiting_team, :event_datetime).with_message("** DOUBLE EVENT **") }
The default error message of uniqueness is “has already been taken”.唯一性的默认错误消息是“已经被采用”。
In case you follow rubocops ImplicitExpect you might what to use something like this:如果您关注rubocops ImplicitExpect ,您可能会使用以下内容:
it { is_expected.to validate(%i[home_team], uniqueness: { scope: [:visiting_team, :event_datetime] }) }
or或者
it { is_expected.to validate_uniqueness_of(:home_team).scoped_to(:visiting_team, :event_datetime) }
(note that I did not include the custom error message as the original question is very old and my purpose is to help visitors from google rather than the original question author) (请注意,我没有包含自定义错误消息,因为原始问题非常古老,我的目的是帮助来自谷歌的访问者而不是原始问题作者)
let(:line) { build(:line) } # if using factory bot, or you could use fixtures
it {expect(line).to validate_uniqueness_of(:home_team).scoped_to(:visiting_team, :event_datetime) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.