Optimize factory usage for application helper
What does this MR do and why?
This MR replaces create with build_stubbed in spec/helpers/application_helper_spec.rb to optimize the factory usage as recommended in our handbook.
This spec is part of the list .rubocop_todo/rspec/factory_bot/avoid_create.yml.
It was verified that database persistence is not needed using the factory doctor command FDOC=1 bin/rspec spec/helpers/application_helper_spec.rb.
Factory Usage Optimization Results
Before optimization:
- Factory time: 3.605s (14.58% of total time)
After optimization:
- Factory time: 1.7429999999999999s (8.11% of total time)
- Factory time reduced by: 1.862s
- Factory time percentage reduced by: 6.47%
- Overall factory usage improvement: 51.7%
Factory Doctor Output
[TEST PROF INFO] FactoryDoctor enabled (event: "sql.active_record", threshold: 0.01)
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
==> Using precompiled Gitaly binaries from cache
Test environment set up in 1.0861699999995835 seconds
.....................................................................................................................[TEST PROF INFO] FactoryDoctor report
Total (potentially) bad examples: 7
Total wasted time: 00:01.952
ApplicationHelper (./spec/helpers/application_helper_spec.rb:5) (31 records created, 00:01.952)
returns the passed path (./spec/helpers/application_helper_spec.rb:473) – 3 records created, 00:00.214
returns the passed path (./spec/helpers/application_helper_spec.rb:479) – 7 records created, 00:00.709
returns the external storage URL prepended to the path (./spec/helpers/application_helper_spec.rb:495) – 5 records created, 00:00.218
preserves the path query parameters (./spec/helpers/application_helper_spec.rb:499) – 5 records created, 00:00.233
returns does not append a token parameter (./spec/helpers/application_helper_spec.rb:508) – 5 records created, 00:00.224
sets group in the body data elements (./spec/helpers/application_helper_spec.rb:529) – 4 records created, 00:00.186
sets all project and id elements correctly related to the issue (./spec/helpers/application_helper_spec.rb:595) – 2 records created, 00:00.164
Finished in 19.09 seconds (files took 13.87 seconds to load)
117 examples, 0 failures
[TEST PROF INFO] Time spent in factories: 00:03.531 (15.47% of total time)
Factory time after update
[TEST PROF INFO] Time spent in factories: 00:01.743 (8.11% of total time)This MR only replaces the create occurrences mentioned by the Factory Doctor. create is still used in this spec, so this MR did not remove spec/helpers/application_helper_spec.rb from .rubocop_todo/rspec/factory_bot/avoid_create.yml.
References
Relates to: #378910.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.