Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new RSpec/FactoryBot/SyntaxMethods cop. #1215

Merged
merged 1 commit into from Nov 25, 2021

Conversation

leoarnold
Copy link
Contributor

@leoarnold leoarnold commented Nov 23, 2021

FactoryBot provides a mixin called
`FactoryBot::Syntax::Methods'
which allows to replace

FactoryBot.create(:bar)
FactoryBot.build(:bar)
FactoryBot.attributes_for(:bar)

with

create(:bar)
build(:bar)
attributes_for(:bar)

The auto-correction is unsafe as there is no way to tell just from syntax
whether a given example group includes the mixin or not.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded in default/config.yml to the next minor version.

If you have modified an existing cop's configuration options:

  • Set VersionChanged in config/default.yml to the next major version.

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Looks good as is. A few cosmetic notes, and a couple of optional suggestions.
Thanks!

changelog/new_add_new_rspecfactorybotsyntaxmethods_cop.md Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/factory_bot/syntax_methods.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/factory_bot/syntax_methods.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/factory_bot/syntax_methods.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/factory_bot/syntax_methods.rb Outdated Show resolved Hide resolved
Description: Use the shorthands from `FactoryBot::Syntax::Methods` in your specs.
Enabled: pending
SafeAutoCorrect: false
VersionAdded: "<<next>>"
Copy link
Member

Choose a reason for hiding this comment

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

We didn't use that notion in the past, but it's quite a useful trick.

@leoarnold leoarnold force-pushed the leoarnold/factorybot/dsl branch 2 times, most recently from e9fe6d6 to 16ce074 Compare November 24, 2021 10:25
`FactoryBot` provides a mixin called
[`FactoryBot::Syntax::Methods'](https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#rspec)
which allows to replace

```ruby
FactoryBot.create(:bar)
FactoryBot.build(:bar)
FactoryBot.attributes_for(:bar)
```

with

```
create(:bar)
build(:bar)
attributes_for(:bar)
```

The auto-correction is unsafe as there is no way to tell just from syntax
whether a given example group includes the mixin or not.
create_pair
generate
generate_list
null
Copy link
Member

Choose a reason for hiding this comment

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

TIL ❤️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FactoryBot::Syntax::Methods.instance_methods.sort 😉

@pirj pirj merged commit 7cf850d into rubocop:master Nov 25, 2021
@pirj
Copy link
Member

pirj commented Nov 25, 2021

Thank you for the contribution!

@leoarnold leoarnold deleted the leoarnold/factorybot/dsl branch November 26, 2021 12:54
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.

None yet

3 participants