From 78ab27a697ba3368868a413f9c797f6e0eae06ce Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Mon, 15 Sep 2025 22:52:41 +0200 Subject: [PATCH] Fix specs --- spec/services/tables/distribution_spec.rb | 32 ++++++++++++----------- spec/services/tables/shift_spec.rb | 4 +-- spec/services/tables/swap_spec.rb | 6 ++--- spec/services/tables/wheel_swap_spec.rb | 2 +- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/spec/services/tables/distribution_spec.rb b/spec/services/tables/distribution_spec.rb index 912e4b3..7e1e7c6 100644 --- a/spec/services/tables/distribution_spec.rb +++ b/spec/services/tables/distribution_spec.rb @@ -6,24 +6,24 @@ require 'rails_helper' module Tables RSpec.describe Distribution do - describe '#save!' do - - around do |example| - ActsAsTenant.with_tenant(create(:wedding)) do - example.run - end - end + let(:tables_arrangement) { TablesArrangement.create! } + around do |example| + ActsAsTenant.with_tenant(create(:wedding)) do + example.run + end + end + + describe '#save!' do let(:people) { create_list(:guest, 2, status: :invited) } let(:distribution) do - described_class.new(min_per_table: 5, max_per_table: 10).tap{|d| d.random_distribution(people)} + described_class.new(min_per_table: 5, max_per_table: 10, tables_arrangement_id: tables_arrangement.id) + .tap { |d| d.random_distribution(people) } end context 'when tables_arrangement_id is nil' do - - - it { expect { distribution.save! }.to change { TablesArrangement.count }.by(1) } - it { expect { distribution.save! }.to change { Seat.count }.by(2) } + it { expect { distribution.save! }.to change(TablesArrangement, :count).by(1) } + it { expect { distribution.save! }.to change(Seat, :count).by(2) } end context 'when tables_arrangement_id is set' do @@ -34,13 +34,15 @@ module Tables distribution.tables_arrangement_id = existing_arrangement.id end - it { expect { distribution.save! }.not_to change { TablesArrangement.count } } - it { expect { distribution.save! }.to change { Seat.count }.by(1) } + it { expect { distribution.save! }.not_to(change(TablesArrangement, :count)) } + it { expect { distribution.save! }.to change(Seat, :count).by(1) } end end describe '#random_distribution' do - subject(:distribution) { described_class.new(min_per_table: 5, max_per_table: 10) } + subject(:distribution) do + described_class.new(min_per_table: 5, max_per_table: 10, tables_arrangement_id: tables_arrangement.id) + end context 'when there are fewer people than the minimum per table' do it 'creates one table' do diff --git a/spec/services/tables/shift_spec.rb b/spec/services/tables/shift_spec.rb index 39e07f8..ab7be98 100644 --- a/spec/services/tables/shift_spec.rb +++ b/spec/services/tables/shift_spec.rb @@ -17,7 +17,7 @@ module Tables context 'when there are two tables with two people each' do let(:initial_solution) do - Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| + Distribution.new(min_per_table: 2, max_per_table: 2, tables_arrangement_id: nil).tap do |distribution| distribution.tables << Set[:a, :b].to_table distribution.tables << Set[:c, :d].to_table end @@ -35,7 +35,7 @@ module Tables context 'when there are two tables with three people each' do let(:initial_solution) do - Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution| + Distribution.new(min_per_table: 3, max_per_table: 3, tables_arrangement_id: nil).tap do |distribution| distribution.tables << Set[:a, :b, :c].to_table distribution.tables << Set[:d, :e, :f].to_table end diff --git a/spec/services/tables/swap_spec.rb b/spec/services/tables/swap_spec.rb index d8db4a7..00e978f 100644 --- a/spec/services/tables/swap_spec.rb +++ b/spec/services/tables/swap_spec.rb @@ -17,7 +17,7 @@ module Tables context 'when there are two tables with two people each' do let(:initial_solution) do - Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| + Distribution.new(min_per_table: 2, max_per_table: 2, tables_arrangement_id: nil).tap do |distribution| distribution.tables << Set[:a, :b].to_table distribution.tables << Set[:c, :d].to_table end @@ -35,7 +35,7 @@ module Tables context 'when there are two tables with three people each' do let(:initial_solution) do - Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution| + Distribution.new(min_per_table: 3, max_per_table: 3, tables_arrangement_id: nil).tap do |distribution| distribution.tables << Set[:a, :b, :c].to_table distribution.tables << Set[:d, :e, :f].to_table end @@ -58,7 +58,7 @@ module Tables context 'when there are three tables with two people each' do let(:initial_solution) do - Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| + Distribution.new(min_per_table: 2, max_per_table: 2, tables_arrangement_id: nil).tap do |distribution| distribution.tables << Set[:a, :b].to_table distribution.tables << Set[:c, :d].to_table distribution.tables << Set[:e, :f].to_table diff --git a/spec/services/tables/wheel_swap_spec.rb b/spec/services/tables/wheel_swap_spec.rb index 02dc161..7ea67db 100644 --- a/spec/services/tables/wheel_swap_spec.rb +++ b/spec/services/tables/wheel_swap_spec.rb @@ -8,7 +8,7 @@ module Tables RSpec.describe WheelSwap do context 'when the solution has three tables' do let(:initial_solution) do - Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution| + Distribution.new(min_per_table: 3, max_per_table: 3, tables_arrangement_id: nil).tap do |distribution| distribution.tables << Set[:a, :b, :c].to_table distribution.tables << Set[:d, :e, :f].to_table distribution.tables << Set[:g, :h, :i].to_table