Skip to content

Comments

Update RSpec/SharedExamples to allow specifying a preferred method to use for defining and including shared examples and context#2062

Closed
dvandersluis wants to merge 1 commit intomasterfrom
shared-examples-method
Closed

Update RSpec/SharedExamples to allow specifying a preferred method to use for defining and including shared examples and context#2062
dvandersluis wants to merge 1 commit intomasterfrom
shared-examples-method

Conversation

@dvandersluis
Copy link
Member

Allows RSpec/SharedExamples to be configured with preferred methods for defining and including shared examples and context, so that consistency can be enforced.

Follows rubocop/rubocop#14033.


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 is configured as Enabled: true in .rubocop.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: "<<next>>" in default/config.yml.

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

  • Set VersionChanged: "<<next>>" in config/default.yml.

@dvandersluis dvandersluis requested a review from a team as a code owner March 25, 2025 19:05
Comment on lines +119 to +123
elsif Includes.context(method_name)
# :nocov: - simplecov is not detecting the coverage of this branch!
cop_config['PreferredIncludeContextMethod']
# :nocov:
end
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm unclear as to why, but simplecov doesn't think this branch is covered, even though it is (line 270 of the spec).

@dvandersluis
Copy link
Member Author

Failing edge rubocop check is fixed in #2063.

@pirj
Copy link
Member

pirj commented Mar 25, 2025

Nice!

Glancing over, it feels this would better be a separate cop. Do you have a specific reason to modify the existing one?

Once concern that comes to mind is that new cops are pending by default. We're releasing major versions more often, and in less than a year this cop will be enabled for everyone.

@dvandersluis
Copy link
Member Author

@pirj ha! I went back and forth on if it should enhance the existing cop or create a new one. I settled on updating because RSpec/SharedExamples seems like the right name for this and I wasn't sure what to name a new cop as. Do you have a suggestion?

…to use for defining and including shared examples and context
@dvandersluis dvandersluis force-pushed the shared-examples-method branch from 313e4e5 to 9144216 Compare March 25, 2025 19:20
@pirj
Copy link
Member

pirj commented Mar 25, 2025

I don't think we had a chance to adjust cop names to follow the cop naming guidelines, but I'd call those two RSpec/IncorrectSharedExampleAlias and RSpec/IncorrectSharedExampleHandleType. Now they look more like Java class names, though.

@dvandersluis
Copy link
Member Author

How about for these changes RSpec/PreferredSharedExampleMethods?

@dvandersluis
Copy link
Member Author

I'll open a new PR with the new cop.

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