Reputation: 4271
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?
Upvotes: 26
Views: 19704
Reputation: 1569
In case you follow rubocops ImplicitExpect you might what to use something like this:
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)
Upvotes: 4
Reputation: 35585
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) }
Upvotes: 1
Reputation: 499
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”.
Upvotes: 49