Skip to content

Update FactoryBot Static Attributes to Dynamic - #51336

Merged
Hamms merged 2 commits into
stagingfrom
apply-FactoryBot/AttributeDefinedStatically
Apr 17, 2023
Merged

Update FactoryBot Static Attributes to Dynamic#51336
Hamms merged 2 commits into
stagingfrom
apply-FactoryBot/AttributeDefinedStatically

Conversation

@Hamms

@Hamms Hamms commented Apr 13, 2023

Copy link
Copy Markdown
Contributor

In preparation for an update to FactoryBot 5.x (and beyond), which deprecated static attributes in favor of dynamic attributes

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

diff --git a/.rubocop.yml b/.rubocop.yml
index 041a14ac898..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 81ed2677555..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.

Links

Testing story

Relying on our existing tests to verify that this update to test configuration syntax does not result in any functional changes.

Follow-up work

After this, we should be ready to upgrade to FactoryBot 5.x

Hamms added 2 commits April 13, 2023 14:53
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
@Hamms
Hamms marked this pull request as ready for review April 14, 2023 18:04
@Hamms
Hamms requested review from a team April 14, 2023 18:04

@cat5inthecradle cat5inthecradle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - I trust the process on this being valid. Looks good scanning through it, though I've admittedly never used FactoryBot.

@Hamms
Hamms merged commit 63ae39e into staging Apr 17, 2023
@Hamms
Hamms deleted the apply-FactoryBot/AttributeDefinedStatically branch April 17, 2023 19:43
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.

2 participants