Skip to content

Commit

Permalink
Make WorkPackage factory use default factory for type if one is defined
Browse files Browse the repository at this point in the history
With TestProf, it's possible to define a default factory for a given
factory using `FactoryBot.set_factory_default`. It activates if a
factory is set for a given field, for instance `author factory: :user`.

For work packages, there is no such factory defined for `type` field
because we have a `after_build` hook to use the first project's type.
Nevertheless, when a default factory has been set, we want to use it
instead, especially as getting the first type is not deterministic when
there are multiple types defined.

This commit makes the WorkPackage factory use this factory default for
the `type` field if one is defined.

This fixes flaky test `spec/services/relations/delete_service_spec.rb`
where the wrong type would be used instead of the intended one. I also
updated the position of the type with mandatory custom field to be after
the type task so that it would work even without the default factory.
  • Loading branch information
cbliard committed Jan 21, 2025
1 parent 0ea9d00 commit e2f7a92
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion spec/factories/work_package_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
end

callback(:after_build) do |work_package, evaluator|
work_package.type = work_package.project.types.first unless work_package.type
work_package.type ||= TestProf::FactoryBot.get_factory_default(:type) || work_package.project.types.first

custom_values = evaluator.custom_values || {}

Expand Down
1 change: 1 addition & 0 deletions spec/services/relations/delete_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
shared_let(:type_task) { create(:type_task, :default) }
shared_let(:type_with_mandatory_cf) do
create(:type,
position: type_task.position + 1,
name: "Type with mandatory custom field",
custom_fields: [mandatory_custom_field])
end
Expand Down

0 comments on commit e2f7a92

Please sign in to comment.