Files
ruby-sea-battle/main-server/spec/models/match_player_spec.rb
T

41 lines
1.4 KiB
Ruby

require "rails_helper"
RSpec.describe MatchPlayer, type: :model do
let(:player1) { User.create!(nickname: "player_one", password: "secret123") }
let(:player2) { User.create!(nickname: "player_two", password: "secret123") }
let(:other_player) { User.create!(nickname: "player_three", password: "secret123") }
let(:match) do
Match.create!(
player1: player1,
player2: player2,
status: :ongoing,
game_server: "ws://game-server:8080/ws",
started_at: Time.current,
player1_rating_before: 100,
player2_rating_before: 100,
player1_rating_change: 0,
player2_rating_change: 0
)
end
it "belongs to a match by uuid and a player by id" do
match_player = match.match_players.find_by(player: player1)
expect(match_player.match).to eq(match)
expect(match_player.player).to eq(player1)
expect(match_player.match_uuid).to eq(match.uuid)
expect(match_player.player_id).to eq(player1.id)
end
it "validates uniqueness of player_id scoped to match_uuid" do
duplicate = MatchPlayer.new(match_uuid: match.uuid, player_id: player1.id)
expect(duplicate).not_to be_valid
expect(duplicate.errors[:player_id]).to be_present
end
it "requires match_uuid and player_id" do
expect(MatchPlayer.new(match_uuid: nil, player_id: player1.id)).not_to be_valid
expect(MatchPlayer.new(match_uuid: match.uuid, player_id: nil)).not_to be_valid
end
end