Skip to content

Commit

Permalink
Merge pull request #17 from marcel-strzalka/feature/better-specs
Browse files Browse the repository at this point in the history
Feature/better specs
  • Loading branch information
marcel-strzalka authored Oct 25, 2022
2 parents 6983132 + 3267ced commit fe581e4
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 59 deletions.
11 changes: 6 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require: rubocop-rails
require:
- rubocop-rails
- rubocop-rspec

AllCops:
TargetRubyVersion: 3.1.2
Expand All @@ -18,10 +20,6 @@ Layout/MultilineMethodCallIndentation:
Metrics/AbcSize:
Max: 20

Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'

Metrics/MethodLength:
Max: 15

Expand All @@ -32,5 +30,8 @@ Rails/DeprecatedActiveModelErrorsMethods:
Rails/I18nLocaleTexts:
Enabled: false

RSpec/MultipleMemoizedHelpers:
Enabled: false

Style/Documentation:
Enabled: false
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ group :development, :test do
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'factory_bot_rails', '~> 6.2.0'
gem 'rspec-rails', '~> 5.1.2'
gem 'rubocop-rspec', '~> 2.14.0'
end

group :development do
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ GEM
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop-rspec (2.14.1)
rubocop (~> 1.33)
ruby-progressbar (1.11.0)
sprockets (4.1.1)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -311,6 +313,7 @@ DEPENDENCIES
rspec-rails (~> 5.1.2)
rubocop (~> 1.36.0)
rubocop-rails (~> 2.16.0)
rubocop-rspec (~> 2.14.0)
sprockets-rails
stimulus-rails
turbo-rails
Expand Down
10 changes: 5 additions & 5 deletions spec/services/cancel_reservation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
require 'rails_helper'

RSpec.describe CancelReservation do
subject { described_class.new(reservation:).call }
subject(:cancel_reservation) { described_class.new(reservation:).call }

describe '#call' do
context 'when reservation is booked' do
let(:reservation) { Reservation.new(status: :booked) }
let(:reservation) { create(:reservation, status: :booked) }

it 'updates status to canceled' do
expect { subject }.to(change { reservation.status }.from('booked').to('canceled'))
expect { cancel_reservation }.to(change(reservation, :status).from('booked').to('canceled'))
end
end

context 'when reservation is not booked' do
let(:reservation) { Reservation.new(status: :canceled) }
let(:reservation) { create(:reservation, status: :canceled) }

it 'does not update status' do
expect { subject }.to_not(change { reservation.status })
expect { cancel_reservation }.not_to(change(reservation, :status))
end
end
end
Expand Down
11 changes: 6 additions & 5 deletions spec/services/confirm_reservation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
require 'rails_helper'

RSpec.describe ConfirmReservation do
subject { described_class.new(reservation:).call }
subject(:confirm_reservation) { described_class.new(reservation:).call }

describe '#call' do
context 'when reservation is booked' do
let(:reservation) { Reservation.new(status: :booked) }
let(:reservation) { create(:reservation, status: :booked) }

it 'updates status to confirmed' do
expect { subject }.to(change { reservation.status }.from('booked').to('confirmed'))
expect { confirm_reservation }.to change(reservation, :status)
.from('booked').to('confirmed')
end
end

context 'when reservation is not booked' do
let(:reservation) { Reservation.new(status: :canceled) }
let(:reservation) { create(:reservation, status: :canceled) }

it 'does not update status' do
expect { subject }.to_not(change { reservation.status })
expect { confirm_reservation }.not_to change(reservation, :status)
end
end
end
Expand Down
92 changes: 48 additions & 44 deletions spec/services/create_reservation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,102 @@
require 'rails_helper'

RSpec.describe CreateReservation do
subject { described_class.new(reservation:, seat_numbers:).call }
subject(:create_reservation) { described_class.new(reservation:, seat_numbers:).call }

let(:hall) { create :hall }
let(:movie) { create :movie }
let(:show) { create :show }
let(:user) { create :user }
let(:hall) { create(:hall) }
let(:movie) { create(:movie) }
let(:show) { create(:show) }
let(:user) { create(:user) }
let(:reservation) { build(:reservation, show:, user:) }

describe '#call' do
context 'when seat numbers are empty' do
let(:seat_numbers) { [] }

it 'returns false' do
expect(subject).to be(false)
expect(create_reservation).to be(false)
end

it 'does not create a reservation' do
expect { subject }.to_not(change { Reservation.count })
expect { create_reservation }.not_to(change(Reservation, :count))
end

it 'adds an error' do
subject
create_reservation
expect(reservation.errors[:base]).to include('You need to pick at least one seat')
end
end

context 'when seat number is invalid' do
shared_examples 'invalid seat number' do |seat_number|
let(:seat_numbers) { [seat_number] }
shared_examples 'invalid seat number' do |seat_number|
let(:seat_numbers) { [seat_number] }

it 'returns false' do
expect(subject).to be(false)
end

it 'does not create a reservation' do
expect { subject }.to_not(change { Reservation.count })
end

it 'adds an error' do
subject
expect(reservation.errors[:base]).to include('One of provided seats is invalid')
end
it 'returns false' do
expect(create_reservation).to be(false)
end

context 'when seat number is less than 1' do
include_examples 'invalid seat number', '0'
it 'does not create a reservation' do
expect { create_reservation }.not_to(change(Reservation, :count))
end

context 'when seat number is greater than hall capacity' do
include_examples 'invalid seat number', '3'
it 'adds an error' do
create_reservation
expect(reservation.errors[:base]).to include('One of provided seats is invalid')
end
end

context 'when seat number is not a number' do
include_examples 'invalid seat number', 'A'
end
context 'when seat number is less than 1' do
include_examples 'invalid seat number', '0'
end

context 'when seat number is greater than hall capacity' do
include_examples 'invalid seat number', '3'
end

context 'when seat number is not a number' do
include_examples 'invalid seat number', 'A'
end

context 'when seat number is taken' do
let!(:existing_reservation) { create(:reservation, show:, user:) }
let!(:existing_ticket) { create(:ticket, reservation:, seat_number: 1) }
before do
create(:reservation, show:, user:)
create(:ticket, reservation:, seat_number: 1)
end

let(:seat_numbers) { ['1'] }

it 'returns false' do
expect(subject).to be(false)
expect(create_reservation).to be(false)
end

it 'does not create a reservation' do
expect { subject }.to_not(change { Reservation.count })
expect { create_reservation }.not_to(change(Reservation, :count))
end

it 'adds an error' do
subject
create_reservation
expect(reservation.errors[:base]).to include('One of provided seats is already taken')
end
end

context 'when every seat is taken' do
let!(:existing_reservation) { create(:reservation, show:, user:) }
let!(:existing_first_ticket) { create(:ticket, reservation:, seat_number: 1) }
let!(:existing_second_ticket) { create(:ticket, reservation:, seat_number: 2) }
before do
create(:reservation, show:, user:)
create(:ticket, reservation:, seat_number: 1)
create(:ticket, reservation:, seat_number: 2)
end

let(:seat_numbers) { %w[1 2] }

it 'returns false' do
expect(subject).to be(false)
expect(create_reservation).to be(false)
end

it 'does not create a reservation' do
expect { subject }.to_not(change { Reservation.count })
expect { create_reservation }.not_to(change(Reservation, :count))
end

it 'adds an error' do
subject
create_reservation
expect(reservation.errors[:base]).to include('Sorry, no seats are available for this show')
end
end
Expand All @@ -103,15 +107,15 @@
let(:seat_numbers) { %w[1 2] }

it 'returns true' do
expect(subject).to be(true)
expect(create_reservation).to be(true)
end

it 'creates a reservation' do
expect { subject }.to change { Reservation.count }.by(1)
expect { create_reservation }.to change(Reservation, :count).by(1)
end

it 'creates 2 tickets' do
expect { subject }.to change { Ticket.count }.by(2)
expect { create_reservation }.to change(Ticket, :count).by(2)
end
end
end
Expand Down

0 comments on commit fe581e4

Please sign in to comment.