Skip to content

Factory bot 5.2.0 use parent strategy false - #51338

Closed
Hamms wants to merge 12 commits into
stagingfrom
factory_bot-5.2.0-use_parent_strategy-false
Closed

Factory bot 5.2.0 use parent strategy false#51338
Hamms wants to merge 12 commits into
stagingfrom
factory_bot-5.2.0-use_parent_strategy-false

Conversation

@Hamms

@Hamms Hamms commented Apr 13, 2023

Copy link
Copy Markdown
Contributor

Warning!!

The AP CSP Create Performance Task is in progress from April 1 - May 1 2023. Please
consider any risk introduced by this PR that could affect our students taking AP CSP. Code.org
students taking AP CSP primarily use App Lab for their Create Task, however a small percent use Game
Lab. Carefully consider whether your change has any risk of alterering, changing, or breaking
anything in these two labs. Even small changes, such as a different button color, are considered
significant during this time period. Reach out to the Student Learning team or Curriculum team for
more details.

Links

Testing story

Deployment strategy

Follow-up work

Privacy

Security

Caching

PR Checklist:

  • Tests provide adequate coverage
  • Privacy and Security impacts have been assessed
  • Code is well-commented
  • New features are translatable or updates will not break translations
  • Relevant documentation has been added or updated
  • User impact is well-understood and desirable
  • Pull Request is labeled appropriately
  • Follow-up work items (including potential tech debt) are tracked and linked

Hamms added 12 commits April 13, 2023 12:08
Specifically target the latest 5.x version as a transitional step toward v6.2, which adds support for Ruby 3.

Breaking changes in v5:

- Removed support for EOL versions of Ruby and Rails
- Removed static attributes (use dynamic attributes with a block instead)
- Removed looking up factories by class
- Removed ignore method (use transient instead)
- Removed duplicate_attribute_assignment_from_initialize_with configuration option

https://github.com/thoughtbot/factory_bot/releases/tag/v5.0.0
In preparation for an update to FactoryBot 5.x (and beyond), which deprecated static attributes in favor of dynamic attributes. Update

Update applied automatically by temporarily configuring my local environment to enable the `FactoryBot/AttributeDefinedStatically` lint rule:

```diff
diff --git a/.rubocop.yml b/.rubocop.yml
index 041a14a..594b15160a3 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -6,6 +6,7 @@ inherit_from:
 require:
   - rubocop-performance
   - rubocop-rails
+  - rubocop-rspec
   - rubocop-rails-accessibility
   - './tools/customLinters/rubocop_only_allowed_characters.rb'

@@ -74,6 +75,10 @@ Style/Semicolon:
 Style/WordArray:
   MinSize: 5

+RSpec/FactoryBot/AttributeDefinedStatically:
+  Include:
+    - 'dashboard/test/factories/*.rb'
+
 # END CODE.ORG OVERRIDES

 # BEGIN BLOCKLIST: Below are rules we don't plan to enable in the forseeable
diff --git a/Gemfile b/Gemfile
index 81ed267..917649bcb73 100644
--- a/Gemfile
+++ b/Gemfile
@@ -242,6 +242,7 @@ group :development, :staging, :levelbuilder do
   gem 'rubocop-performance', require: false
   gem 'rubocop-rails', require: false
   gem 'rubocop-rails-accessibility', require: false
+  gem 'rubocop-rspec', require: false
   gem 'scss_lint', require: false
 end
```

And then running `rubocop --require rubocop-rspec --only RSpec/FactoryBot/AttributeDefinedStatically --auto-correct dashboard/test/factories/*`. I then used `rubocop --only Layout/SpaceInsideBlockBraces --auto-correct dashboard/test/factories/*.rb` to automatically fix some white space errors, and manually edited `dashboard/test/factories/foorm_factories.rb` to fix up a few more.

- https://thoughtbot.com/blog/deprecating-static-attributes-in-factory_bot-4-11
- https://docs.rubocop.org/rubocop-rspec/cops_rspec_factorybot.html#rspecfactorybotattributedefinedstatically
In preparation for an update to FactoryBot 5, which fixes some unintuitive behavior when `build`ing objects with descendents. Specifically, the `:pd_teacher_application` factory calls `build :pd_teacher_application_hash_common` which in turn calls `association :school`; right now, even though the Hash is being called with `build` rather than `create`, that `School` object still gets persisted. In fact, that behavior is *required* since we pass the `id` of the `School` object upstream to the `:pd_teacher_application` factory which as part of its own persistence logic expects its `school_id` attribute to reference a persisted object.

Startingin FactoryBot 5, the default association strategy will now be inherited from the parent factory, so `build`ing a hash will `build` but not persist the associated `School` object. In most cases this is probably desirable, but here we are relying on the old strange behavior and so now need to explicitly specify that we always want to persist the associated `School` object.

https://github.com/thoughtbot/factory_bot/blob/main/GETTING_STARTED.md#build-strategies-1
@Hamms

Hamms commented Apr 27, 2023

Copy link
Copy Markdown
Contributor Author

closing in favor of #50912

@Hamms Hamms closed this Apr 27, 2023
@Hamms
Hamms deleted the factory_bot-5.2.0-use_parent_strategy-false branch April 27, 2023 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant