What am I doing wrong here? I'm pretty sure the actual code is right but there's something wrong with my tests Is this just a fail in Shoulda Matchers? Or am I just missing something?
require 'rails_helper'
RSpec.describe Game, type: :model do
subject { create :game }
it { should belong_to(:home_team).class_name('Team') }
it { should belong_to(:away_team).class_name('Team') }
it { should validate_uniqueness_of(:start_date).scoped_to(:home_team_id) }
it { should validate_uniqueness_of(:start_date).scoped_to(:away_team_id) }
end
class Game < ActiveRecord::Base
belongs_to :home_team, class_name: 'Team'
belongs_to :away_team, class_name: 'Team'
validates_uniqueness_of :start_date, scope: :home_team_id
validates_uniqueness_of :start_date, scope: :away_team_id
end
Expected Game to validate that :start_date is case-sensitively unique
within the scope of :home_team_id, but this could not be proved.
Given an existing Game whose :start_date is ‹Fri, 31 Aug 2018 05:24:25
UTC +00:00›, after making a new Game and setting its :start_date to
‹Fri, 31 Aug 2018 05:24:25 UTC +00:00› as well and its :home_team_id
to a different value, ‹5›, the matcher expected the new Game to be
valid, but it was invalid instead, producing these validation errors:
* start_date: ["has already been taken"]
Sorry, my previous answer was incorrect. Looks like shoulda can't hadle your case automatically. It changes
home_team_idand expects that the game is valid, but it still has the sameaway_team_id, so you need to validate it manually