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 a cop to check for consistent method usage in feature specs. #439

Merged
merged 5 commits into from
Sep 8, 2017

Conversation

rspeicher
Copy link
Contributor

No description provided.

@rspeicher rspeicher force-pushed the rs-feature_methods branch from 998bc6d to 1208b18 Compare August 5, 2017 18:25
@rspeicher rspeicher changed the title WIP: Add a cop to check for consistent method usage in feature specs. Add a cop to check for consistent method usage in feature specs. Aug 5, 2017
@rspeicher rspeicher force-pushed the rs-feature_methods branch from 1208b18 to a443b44 Compare August 5, 2017 18:32
@rspeicher
Copy link
Contributor Author

I started down the path of making this ConfigurableEnforcedStyle, with rspec and capybara options.

If someone were to set capybara, we'd have to make sure to only check:

  1. Files in spec/features
  2. Files with type: :feature (and possibly just :feature?) metadata

I wasn't sure of the best way to handle those two conditions, so I'm open to feedback on making this configurable.

class FeatureMethods < Cop
MSG = 'Use `%s` instead of `%s`.'.freeze

# https://git.io/v7rLu
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you hit y on your keyboard when visiting this link then github will give you a link to a sha that will never break. https://help.github.com/articles/getting-permanent-links-to-files/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, updated.

@rspeicher rspeicher force-pushed the rs-feature_methods branch from a443b44 to 92d251c Compare August 7, 2017 16:10
Copy link
Collaborator

@backus backus left a comment

Choose a reason for hiding this comment

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

Nice!

@bquorning
Copy link
Collaborator

I’ve been trying to find out why capybara provides these aliased methods. The README just says that “Capybara also comes with a built in DSL for creating descriptive acceptance tests […] feature is in fact just an alias for describe ..., :type => :feature, background is an alias for before, scenario for it, and given/given! aliases for let/let!, respectively.” without any explanation for why this DSL should be preferable.

Also, I just want to note that these aliases are only available if Capybara (or rspec-rails) is loaded. Which puts this cop in the same category as RSpec::FactoryGirl::DynamicAttributeDefinedStatically as “not really an RSpec cop, but close”. Perhaps we should have an RSpec::Capybara module?

@rspeicher
Copy link
Contributor Author

@bquorning I believe they're just syntactic sugar to make feature specs read more like Cucumber features.

@Darhazer
Copy link
Member

Yup, this should be in RSpec::Capybara module. There are few other things we could implement for capybara (like #378 for example)

@rspeicher
Copy link
Contributor Author

rspeicher commented Aug 22, 2017

Moved the cop to a new Capybara namespace.

'factory_girl' => 'FactoryGirl'
}
glob = SpecHelper::ROOT.join('lib', 'rubocop', 'cop',
'rspec', '{,factory_girl/}*.rb')
'rspec', '{capybara/,,factory_girl/}*.rb')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Requiring these two changes might get messy quickly if you anticipate adding more namespaces, but I went with the simplest thing for now.

@rspeicher
Copy link
Contributor Author

Can't see what the build failures are since they're private.

@Darhazer
Copy link
Member

@rspeicher you can run rake on your local machine to check the build. I think you should add Capybara to the namespaces in ConfigFormatter and fix the build_config script to include capybara

-cops = '{*.rb,factory_girl/*.rb}'
+cops = '{*.rb,factory_girl/*.rb,capybara/*.rb}'

@bquorning
Copy link
Collaborator

Ahh, I think you need to add ,capybara/*.rb to bin/build_config for the confirm_config test to pass.

@rspeicher
Copy link
Contributor Author

Back to green, thanks y'all!

Capybara/FeatureMethods:
Description: Checks for consistent method usage in feature specs.
Enabled: true
StyleGuide: http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/Capybara/FeatureMethods
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this link and the one below it are wrong -- the namespace should be RuboCop/Cop/RSpec/{Capybara,FactoryGirl}, no?

@@ -8,7 +8,7 @@ require 'rubocop-rspec'
require 'rubocop/rspec/description_extractor'
require 'rubocop/rspec/config_formatter'

cops = '{*.rb,factory_girl/*.rb}'
cops = '{*.rb,capybara/*.rb,factory_girl/*.rb}'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This could be simplified to just **/*.rb.

@rspeicher
Copy link
Contributor Author

Rebased since 1.16.0 was released.

All cops need to be nested under `RuboCop/Cop/RSpec`, but cops nested
under `Capybara` and `FactoryGirl` were missing the `RSpec` part.
@@ -22,7 +22,7 @@ def unified_config
cops.each_with_object(config.dup) do |cop, unified|
unified[cop] = config.fetch(cop)
.merge(descriptions.fetch(cop))
.merge('StyleGuide' => STYLE_GUIDE_BASE_URL + cop)
.merge('StyleGuide' => STYLE_GUIDE_BASE_URL + cop.sub('RSpec/', ''))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would you prefer an anchored regex here?

@Darhazer
Copy link
Member

Darhazer commented Sep 8, 2017

I think it's ready for merge. @backus final words?

@backus
Copy link
Collaborator

backus commented Sep 8, 2017

LGTM. @rspeicher or @Darhazer (whoever does the merge) can you squash down the commits?

@Darhazer Darhazer merged commit 7fe393a into rubocop:master Sep 8, 2017
@bquorning bquorning mentioned this pull request Oct 5, 2018
5 tasks
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.

4 participants