对不起,我以前的回答不正确。看起来Shoulda不能自动处理你的案子。它改变
home_team_id
希望游戏是有效的,但它还是一样的
away_team_id
,因此您需要手动验证它
it 'validates uniqueness for both teams' do
team1 = create :team
team2 = create :team
team3 = create :team
game = create :game, home_team: team1, away_team: team2
game1 = build :game, home_team: team1, away_team: team3
game2 = build :game, home_team: team3, away_team: team2
game3 = build :game, home_team: team2, away_team: team1
expect(game).to be_valid
expect(game1).not_to be_valid
expect(game2).not_to be_valid
expect(game3).to be_valid
end