Rspec validate uniqueness with scope

slindsey3000 picture slindsey3000 · May 7, 2013 · Viewed 12.8k times · Source

I have this in my Line model

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?

Answer

Roy picture Roy · May 8, 2013

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”.