diff --git a/modules/backlogs/spec/models/work_packages/position_spec.rb b/modules/backlogs/spec/models/work_packages/position_spec.rb index 8b85499af92..b948f1ad786 100644 --- a/modules/backlogs/spec/models/work_packages/position_spec.rb +++ b/modules/backlogs/spec/models/work_packages/position_spec.rb @@ -79,7 +79,7 @@ RSpec.describe WorkPackage, "positions" do # rubocop:disable RSpec/SpecFilePathF end def have_positions(**) # rubocop:disable Naming/PredicatePrefix - have_attr(:position, identified_by: :subject, **) + pluck(:position, identified_by: :subject).eq(**) end context "when creating a work_package in a sprint" do diff --git a/modules/backlogs/spec/services/backlogs/work_packages/rebuild_positions_service_integration_spec.rb b/modules/backlogs/spec/services/backlogs/work_packages/rebuild_positions_service_integration_spec.rb index ee08ca0b527..82f617010e7 100644 --- a/modules/backlogs/spec/services/backlogs/work_packages/rebuild_positions_service_integration_spec.rb +++ b/modules/backlogs/spec/services/backlogs/work_packages/rebuild_positions_service_integration_spec.rb @@ -76,7 +76,7 @@ RSpec.describe Backlogs::WorkPackages::RebuildPositionsService, "integration", t shared_let(:bucket2_wp3) { create_work_package(subject: "Bucket 2 WorkPackage 3", backlog_bucket: bucket2, position: nil) } def have_positions(**) # rubocop:disable Naming/PredicatePrefix - have_attr(:position, identified_by: :subject, **) + pluck(:position).eq(**) end context "with the project provided" do diff --git a/spec/support/matchers/have_attr.rb b/spec/support/matchers/pluck.rb similarity index 67% rename from spec/support/matchers/have_attr.rb rename to spec/support/matchers/pluck.rb index 1da3353f9cb..6bc20650d0f 100644 --- a/spec/support/matchers/have_attr.rb +++ b/spec/support/matchers/pluck.rb @@ -30,23 +30,48 @@ # When you need to check an attribute for multiple records: # -# expect(WorkPackage.where(sprint:)).to have_attr(:position, -# wp1 => 1, -# wp2 => 2, -# wp3 => nil +# expect(WorkPackage.where(sprint:)).to pluck(:position).eq( +# sprint1_wp2 => 1, +# sprint1_wp3 => 2, +# sprint1_wp4 => 3, +# sprint1_wp1 => 4, +# sprint1_wp5 => 6 # ) # +# Fails with: +# +# 582 => 1, +# 583 => 2, +# 584 => 3, +# -585 => 6, +# +585 => 5, +# # Specify `identified_by` attribute to be used instead of `id` to differentiate # records in failurediff, it must be unique: # -# expect(WorkPackage.where(sprint:)).to have_attr(:position, identified_by: :subject, -# wp1 => 1, -# wp2 => 2 +# expect(WorkPackage.where(sprint:)).to pluck(:position, identified_by: :subject).eq( +# sprint1_wp2 => 1, +# sprint1_wp3 => 2, +# sprint1_wp4 => 3, +# sprint1_wp1 => 4, +# sprint1_wp5 => 6 # ) -RSpec::Matchers.define :have_attr do |attribute, identified_by: :id, **expected| - match do |actual| - @actual = actual.pluck(identified_by, attribute).to_h +# +# Fails with: +# +# "Sprint 1 WorkPackage 2" => 1, +# "Sprint 1 WorkPackage 3" => 2, +# "Sprint 1 WorkPackage 4" => 3, +# -"Sprint 1 WorkPackage 5" => 6, +# +"Sprint 1 WorkPackage 5" => 5, + +RSpec::Matchers.define :pluck do |attribute, identified_by: :id| + chain :eq do |expected| @expected = expected.transform_keys { it.public_send(identified_by) } + end + + match do |actual| + @actual = actual.pluck(identified_by, attribute).to_h @actual == @expected end