Class: RuboCop::Cop::RSpec::Capybara::CurrentPathExpectation

Inherits:
RuboCop::Cop::RSpec::Cop show all
Defined in:
lib/rubocop/cop/rspec/capybara/current_path_expectation.rb

Overview

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.

Examples:

# 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/)

Constant Summary collapse

MSG =
'Do not set an RSpec expectation on `current_path` in ' \
'Capybara feature specs - instead, use the ' \
'`have_current_path` matcher on `page`'

Constants inherited from RuboCop::Cop::RSpec::Cop

RuboCop::Cop::RSpec::Cop::DEFAULT_CONFIGURATION, RuboCop::Cop::RSpec::Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods inherited from RuboCop::Cop::RSpec::Cop

inherited, #relevant_file?

Instance Method Details

#autocorrect(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Object



48
49
50
51
52
# 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