2013-05-07 107 views
13

我有這個在我的生產線示範Rspec的驗證唯一性範圍

validates :home_team, :uniqueness => { :scope => [:visiting_team, :event_datetime], 
:message => "** DOUBLE EVENT **" } 

我有這個在我的規格

describe Line do 
    it { should validate_uniqueness_of(:home_team).scoped_to(:visiting_team, :event_datetime) } 

我得到這個錯誤...

失敗:

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)>' 

任何想法,爲什麼這是失敗?

回答

28

我認爲你需要做到這一點讓它通過

it { should validate_uniqueness_of(:home_team).scoped_to(:visiting_team, :event_datetime).with_message("** DOUBLE EVENT **") } 

是「已拍攝的」獨特的默認的錯誤消息。

+0

嗨羅伊:)現在我得到了......我想我將不得不嘗試新的方法。 1)線路 故障/錯誤:它{應當validate_uniqueness_of(:HOME_TEAM).scoped_to(:visiting_team,:event_datetime).with_message( 「**雙次事件**」)} NoMethodError: 未定義的方法'變化」的1:Fixnum #./spec/models/line_spec.rb:7:in ' – slindsey3000 2013-05-08 14:30:39

+0

這也適用於我,但是如果你想驗證唯一性呢'除非: - > {home_team.nil? '',你會如何測試? – Caleb 2016-04-14 19:25:23