class RuboCop::Cop::RSpec::Capybara::CurrentPathExpectation

Checks that no expectations are set on Capybara's `current_path`.

The `have_current_path` matcher (www.rubydoc.info/github/ teamcapybara/capybara/master/Capybara/RSpecMatchers#have_current_path- instance_method) should be used on `page` to set expectations on Capybara's current path, since it uses Capybara's waiting functionality (github.com/teamcapybara/capybara/blob/master/ README.md#asynchronous-javascript-ajax-and-friends) which ensures that preceding actions (like `click_link`) have completed.

@example

# bad
expect(current_path).to eq('/callback')
expect(page.current_path).to match(/widgets/)

# good
expect(page).to have_current_path("/callback")
expect(page).to have_current_path(/widgets/)

Constants

MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/rspec/capybara/current_path_expectation.rb, line 54
def autocorrect(node)
  lambda do |corrector|
    return unless node.chained?

    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
end
on_send(node) click to toggle source
# File lib/rubocop/cop/rspec/capybara/current_path_expectation.rb, line 48
def on_send(node)
  expectation_set_on_current_path(node) do
    add_offense(node, location: :selector)
  end
end

Private Instance Methods

convert_regexp_str_to_literal(corrector, matcher_node, regexp_str) click to toggle source
# File lib/rubocop/cop/rspec/capybara/current_path_expectation.rb, line 83
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.loc.expression, regexp_expr)
end
rewrite_expectation(corrector, node, to_symbol, matcher_node) click to toggle source
# File lib/rubocop/cop/rspec/capybara/current_path_expectation.rb, line 71
def rewrite_expectation(corrector, node, to_symbol, matcher_node)
  current_path_node = node.first_argument
  corrector.replace(current_path_node.loc.expression, '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)
end