-
-
Notifications
You must be signed in to change notification settings - Fork 278
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 cop for Capybara expectations set on current_path
#470
Conversation
08c92e7
to
e4f5d41
Compare
current_path
current_path
You have to:
|
Will do! Thanks @Darhazer - really helpful list. |
@Darhazer I've managed to do everything except no. 1 - I get the following: $ bin/build_config
/Users/timrogers/Projects/rubocop-rspec/lib/rubocop/rspec/config_formatter.rb:23:in `fetch': key not found: "Capybara/CurrentPathExpectation" (KeyError)
from /Users/timrogers/Projects/rubocop-rspec/lib/rubocop/rspec/config_formatter.rb:23:in `block in unified_config'
from /Users/timrogers/Projects/rubocop-rspec/lib/rubocop/rspec/config_formatter.rb:22:in `each'
from /Users/timrogers/Projects/rubocop-rspec/lib/rubocop/rspec/config_formatter.rb:22:in `each_with_object'
from /Users/timrogers/Projects/rubocop-rspec/lib/rubocop/rspec/config_formatter.rb:22:in `unified_config'
from /Users/timrogers/Projects/rubocop-rspec/lib/rubocop/rspec/config_formatter.rb:16:in `dump'
from bin/build_config:20:in `<main>' Any ideas? I'm sure I'm missing something obvious! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just make sure bin/rake
passes
'`have_current_path` matcher on `page`'.freeze | ||
|
||
def_node_matcher :expectation_set_on_current_path, <<-PATTERN | ||
(send nil :expect {(send nil :current_path) (send (send nil :page) :current_path)}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this could be written as (send nil :expect (send {(send nil :page) nil} :current_path)})
@timrogers you need to add the key in |
adcbe25
to
cf58fa9
Compare
@Darhazer Aha - thanks! All done and I've pushed a finished, squashed version. |
Great job, @timrogers Would you be willing to add another one, to check for |
Yes, sure! |
Note: generally only @bquorning any thoughts? |
That makes sense to me. I was aware that people might be using other
matchers, but where they are they’re likely to see non-deterministic test
results such that they’d be well-advised to change their implementation.
I’ll look at the other cop you suggested tomorrow, and may look at
implementing an autocorrect too - but let’s merge this as-is for now if
that’s okay with @bquorning as this feels like a positive addition.
…On Tue, 26 Sep 2017 at 20:13, Maxim Krizhanovsky ***@***.***> wrote:
Note: generally only eq / match kind of matchers can be replaced with
have_current_path. I didn't see other usages though; and even if someone
uses current_path with some fancy matcher, refactoring to
have_current_path is the right thing to do. So I'm going to merge this as
is, unless there are any objections; the note should be respected however
if someone adds an autocorrect to this.
@bquorning <https://github.com/bquorning> any thoughts?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#470 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAHFpio8Uz4L0G9-6_YFbdGH3DVirUBTks5smUy9gaJpZM4PkV9E>
.
|
config/default.yml
Outdated
@@ -334,3 +334,8 @@ FactoryGirl/DynamicAttributeDefinedStatically: | |||
Description: Prefer declaring dynamic attribute values in a block. | |||
Enabled: true | |||
StyleGuide: http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryGirl/DynamicAttributeDefinedStatically | |||
|
|||
Capybara/CurrentPathExpectation: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a semi-alphabetical list (RSpec/
is before Capybara/
) – could we move this entry to line 328?
PATTERN | ||
|
||
def on_send(node) | ||
expectation_set_on_current_path(node) do |_match| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no captures in the node matcher, so no block arguments are passed: You can remove |_match|
.
RUBY | ||
end | ||
|
||
it 'doesn\'t flag a violation for other references to `current_path`' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: If you use double quotes around the string, you don’t need to escape the '
in the middle. (also in line 18).
Setting expectations on `current_path` in our Capybara feature specs leads to flaky tests. Switching to using [Capybara's `have_current_path` matcher](http://www.rubydoc.info/github/teamcapybara/capybara/master/Capybara/RSpecMatchers#have_current_path-instance_method) helps since it uses Capybara's [waiting functionality](https://github.com/teamcapybara/capybara/blob/master/README.md#asynchronous-javascript-ajax-and-friends) that ensures that preceding actions (like `click_link`) have completed. This cop tells you not to use `expect(current_path).to match(/lol/)` or `expect(page.current_path).to match(/lol)`, encouraging you to use the `have_current_path` matcher instead.
cf58fa9
to
63a1438
Compare
@bquorning Done! |
63a1438
to
95e2e3d
Compare
Add cop for Capybara expectations set on `current_path`
Setting expectations on
current_path
in our Capybara feature specs leads to flaky tests. Switching to using Capybara'shave_current_path
matcher helps since it uses Capybara's waiting functionality that ensures that preceding actions (likeclick_link
) have completed.This cop tells you not to use
expect(current_path).to match(/lol/)
, encouraging you to use thehave_current_path
matcher instead.