Refactor spec to use let instead of class vars
This commit is contained in:
parent
167a2116f1
commit
48fc9fab57
|
|
@ -1,52 +1,60 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
RSpec.describe UserService do
|
RSpec.describe UserService do
|
||||||
before do
|
let(:user) do
|
||||||
@user = create(:user)
|
create(:user)
|
||||||
@service = UserService.new @user
|
end
|
||||||
@team = create(:team)
|
|
||||||
|
let(:user_service) do
|
||||||
|
UserService.new(user)
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:team) do
|
||||||
|
create(:team)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_match(involved_team = team, factory = :playoff_match)
|
||||||
|
create(factory, match_scores: [create(:match_score, team: involved_team)])
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#bet!' do
|
describe '#bet!' do
|
||||||
context 'with an unrelated team' do
|
context 'with an unrelated team' do
|
||||||
it 'throws an exception' do
|
it 'throws an exception' do
|
||||||
expect do
|
expect do
|
||||||
@service.bet! create(:playoff_match), create(:team)
|
user_service.bet! build_match(create(:team)), team
|
||||||
end.to raise_error(UserServiceError, 'The given team is not involved in the given match')
|
end.to raise_error(UserServiceError, 'The given team is not involved in the given match')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with an existing team' do
|
context 'with an existing team' do
|
||||||
let(:match) do
|
let(:match) do
|
||||||
create(:playoff_match, match_scores: [create(:match_score, team: @team)])
|
build_match
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
let!(:bet) do
|
||||||
@bet = @service.bet! match, @team
|
user_service.bet! match, team
|
||||||
@user.reload
|
|
||||||
match.reload
|
|
||||||
@team.reload
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'associates the bet with the given team' do
|
it 'associates the bet with the given team' do
|
||||||
expect(@team.bets).to include(@bet)
|
expect(team.bets.reload).to include(bet)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'associates the bet with the given match' do
|
it 'associates the bet with the given match' do
|
||||||
expect(match.bets).to include(@bet)
|
expect(match.bets.reload).to include(bet)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'associates the bet with the creating user' do
|
it 'associates the bet with the creating user' do
|
||||||
expect(@user.bets).to include(@bet)
|
expect(user.bets.reload).to include(bet)
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with an already existing bet' do
|
context 'with an already existing bet' do
|
||||||
it 'throws an exception' do
|
it 'throws an exception' do
|
||||||
match = create(:playoff_match, match_scores: [create(:match_score, team: @team)])
|
match = build_match
|
||||||
@service.bet! match, @team
|
user_service.bet! match, team
|
||||||
|
user.reload
|
||||||
match.reload
|
match.reload
|
||||||
expect do
|
expect do
|
||||||
@service.bet! match, @team
|
user_service.bet! match, team
|
||||||
end.to raise_error(UserServiceError, 'This user already created a bet on this match')
|
end.to raise_error(UserServiceError, 'This user already created a bet on this match')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -55,22 +63,15 @@ RSpec.describe UserService do
|
||||||
context 'without a team' do
|
context 'without a team' do
|
||||||
context 'on a playoff stage' do
|
context 'on a playoff stage' do
|
||||||
it 'throws an exception' do
|
it 'throws an exception' do
|
||||||
match = create(:playoff_match)
|
|
||||||
match.match_scores << create(:match_score, team: @team)
|
|
||||||
expect do
|
expect do
|
||||||
@service.bet! match, nil
|
user_service.bet! build_match, nil
|
||||||
end.to raise_error(UserServiceError, 'Betting on no team in a playoff match is not supported')
|
end.to raise_error(UserServiceError, 'Betting on no team in a playoff match is not supported')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'on a group stage' do
|
context 'on a group stage' do
|
||||||
it 'succeeds' do
|
it 'succeeds' do
|
||||||
match = create(:group_match)
|
user_service.bet! build_match(team, :group_match), nil
|
||||||
match.match_scores << create(:match_score, team: @team)
|
|
||||||
bet = @service.bet! match, nil
|
|
||||||
match.reload
|
|
||||||
expect(match.bets).to include(bet)
|
|
||||||
expect(@user.bets).to include(bet)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue