Add cop StrategyInCallback to avoid create/build in factories callbacks
What does this MR do and why?
Related #262624 and #374088 (closed)
Add a cop to avoid usage of create
/build
etc. in factories callbacks while we should use association in order to be able to use build or build stubbed in tests.
See note #374088 (comment 1142731187)
Screenshots or screen recordings
Example
spec/factories/alert_management/alerts.rb:20:9: C: RSpec/FactoryBot/StrategyInCallback: Prefer inline association over build. See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories
build(:incident, alert_management_alert: alert, project: alert.project)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/factories/alert_management/alerts.rb:26:49: C: RSpec/FactoryBot/StrategyInCallback: Prefer inline association over create. See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories
alert.alert_assignees.create!(assignee: create(:user))
^^^^^^^^^^^^^
spec/factories/ci/builds.rb:57:24: C: RSpec/FactoryBot/StrategyInCallback: Prefer inline association over create. See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories
build.needs << create(:ci_build_need, build: build, name: evaluator.needed.name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/factories/ci/builds.rb:348:9: C: RSpec/FactoryBot/StrategyInCallback: Prefer inline association over create. See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories
create(:ci_job_artifact, :trace, job: build)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
How to set up and validate locally
bundle exec rubocop --only RSpec/FactoryBot/StrategyInCallback spec/factories ee/spec/factories
.....
609 files inspected, no offenses detected
REVEAL_RUBOCOP_TODO=1 bundle exec rubocop -C false --only RSpec/FactoryBot/StrategyInCallback spec/factories ee/spec/factories
[.. watch some offenses .]
609 files inspected, 267 offenses detected
bundle exec rake rubocop:todo:generate[RSpec/FactoryBot/StrategyInCallback]
# No changes
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Edited by Peter Leitzen