Skip to content

Commit 92cc5ff

Browse files
author
Nuno Silva
committed
Improve autocorrect for Capybara/CurrentPathExpectation
`expect(page.current_path).to eq(EXPECTED_VALUE)` is not directly equivalent to `expect(page).to have_current_path(EXPECTED_VALUE)` In particular, `have_current_path` with no options will include the querystring on the path, while `page.current_path` does not. This fix ensures the option `ignore_query: true` is set on `have_current_path` expect when `EXPECTED_VALUE` is a string or regexp. In those cases, it should be safe to not use this param.
1 parent b521fb2 commit 92cc5ff

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Add `RSpec/ContextMethod` cop, to detect method names in `context`. ([@geniou][])
88
* Update RuboCop dependency to 0.68.1 with support for children matching node pattern syntax. ([@pirj][])
99
* Add `RSpec/EmptyLineAfterExample` cop to check that there is an empty line after example blocks. ([@pirj][])
10+
* Fix `Capybara/CurrentPathExpectation` auto-corrector, to include option `ignore_query: true`. ([@onumis][])
1011

1112
## 1.35.0 (2019-08-02)
1213

@@ -446,3 +447,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
446447
[@schmijos]: https://github.com/schmijos
447448
[@foton]: https://github.com/foton
448449
[@nc-holodakg]: https://github.com/nc-holodakg
450+
[@onumis]: https://github.com/onumis

lib/rubocop/cop/rspec/capybara/current_path_expectation.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,29 @@ def rewrite_expectation(corrector, node, to_symbol, matcher_node)
7878
'have_no_current_path'
7979
end
8080
corrector.replace(matcher_node.loc.selector, matcher_method)
81+
add_ignore_query_options(corrector, node)
8182
end
8283

8384
def convert_regexp_str_to_literal(corrector, matcher_node, regexp_str)
8485
str_node = matcher_node.first_argument
8586
regexp_expr = Regexp.new(regexp_str).inspect
8687
corrector.replace(str_node.loc.expression, regexp_expr)
8788
end
89+
90+
# `have_current_path` with no options will include the querystring
91+
# while `page.current_path` does not.
92+
# This ensures the option `ignore_query: true` is added
93+
# except when the expectation is a regexp or string
94+
def add_ignore_query_options(corrector, node)
95+
expectation_node = node.parent.last_argument
96+
expectation_last_child = expectation_node.children.last
97+
return if %i[regexp str].include?(expectation_last_child.type)
98+
99+
corrector.insert_after(
100+
expectation_last_child.loc.expression,
101+
', ignore_query: true'
102+
)
103+
end
88104
end
89105
end
90106
end

spec/rubocop/cop/rspec/capybara/current_path_expectation_spec.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,38 @@
3131

3232
include_examples 'autocorrect',
3333
'expect(current_path).to eq expected_path',
34-
'expect(page).to have_current_path expected_path'
34+
'expect(page).to have_current_path expected_path, '\
35+
'ignore_query: true'
36+
37+
include_examples 'autocorrect',
38+
'expect(current_path).to eq(expected_path)',
39+
'expect(page).to have_current_path(expected_path, '\
40+
'ignore_query: true)'
41+
42+
include_examples 'autocorrect',
43+
'expect(current_path).to(eq(expected_path))',
44+
'expect(page).to(have_current_path(expected_path, '\
45+
'ignore_query: true))'
46+
47+
include_examples 'autocorrect',
48+
'expect(current_path).to eq(expected_path(f: :b))',
49+
'expect(page).to have_current_path(expected_path(f: :b), '\
50+
'ignore_query: true)'
3551

3652
include_examples 'autocorrect',
3753
'expect(page.current_path).to eq(foo(bar).path)',
38-
'expect(page).to have_current_path(foo(bar).path)'
54+
'expect(page).to have_current_path(foo(bar).path, '\
55+
'ignore_query: true)'
3956

4057
include_examples 'autocorrect',
4158
'expect(current_path).not_to eq expected_path',
42-
'expect(page).to have_no_current_path expected_path'
59+
'expect(page).to have_no_current_path expected_path, '\
60+
'ignore_query: true'
4361

4462
include_examples 'autocorrect',
4563
'expect(current_path).to_not eq expected_path',
46-
'expect(page).to have_no_current_path expected_path'
64+
'expect(page).to have_no_current_path expected_path, '\
65+
'ignore_query: true'
4766

4867
include_examples 'autocorrect',
4968
'expect(page.current_path).to match(/regexp/i)',

0 commit comments

Comments
 (0)