class RuboCop::Cop::RSpec::Capybara::CurrentPathExpectation
expect(page).to have_current_path(/widgets/)
expect(page).to have_current_path(“/callback”)
# good
expect(page.current_path).to match(/widgets/)
expect(current_path).to eq(‘/callback’)
# bad
@example
completed.
which ensures that preceding actions (like ‘click_link`) have
waiting functionality]
current path, since it uses
should be used on `page` to set expectations on Capybara’s
[‘have_current_path` matcher]
The
Checks that no expectations are set on Capybara’s ‘current_path`.
def add_ignore_query_options(corrector, node)
This ensures the option `ignore_query: true` is added
while `page.current_path` does not.
`have_current_path` with no options will include the querystring
def add_ignore_query_options(corrector, node) expectation_node = node.parent.last_argument expectation_last_child = expectation_node.children.last return if %i[regexp str].include?(expectation_last_child.type) corrector.insert_after( expectation_last_child, ', ignore_query: true' ) end
def autocorrect(corrector, node)
def autocorrect(corrector, node) as_is_matcher(node.parent) do |to_sym, matcher_node| rewrite_expectation(corrector, node, to_sym, matcher_node) end regexp_str_matcher(node.parent) do |to_sym, matcher_node, regexp| rewrite_expectation(corrector, node, to_sym, matcher_node) convert_regexp_str_to_literal(corrector, matcher_node, regexp) end end
def convert_regexp_str_to_literal(corrector, matcher_node, regexp_str)
def convert_regexp_str_to_literal(corrector, matcher_node, regexp_str) str_node = matcher_node.first_argument regexp_expr = Regexp.new(regexp_str).inspect corrector.replace(str_node, regexp_expr) end
def on_send(node)
def on_send(node) expectation_set_on_current_path(node) do add_offense(node.loc.selector) do |corrector| next unless node.chained? autocorrect(corrector, node) end end end
def rewrite_expectation(corrector, node, to_symbol, matcher_node)
def rewrite_expectation(corrector, node, to_symbol, matcher_node) current_path_node = node.first_argument corrector.replace(current_path_node, 'page') corrector.replace(node.parent.loc.selector, 'to') matcher_method = if to_symbol == :to 'have_current_path' else 'have_no_current_path' end corrector.replace(matcher_node.loc.selector, matcher_method) add_ignore_query_options(corrector, node) end