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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/capybara/current_path_expectation.rb

Overview

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

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

This cop does not support autocorrection in some cases.

Examples:

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

# good
expect(page).to have_current_path('/callback', ignore_query: true)

# bad (does not support autocorrection when `match` with a variable)
expect(page).to match(variable)

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`'
RESTRICT_ON_SEND =
%i[expect].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.autocorrect_incompatible_withObject



59
60
61
# File 'lib/rubocop/cop/capybara/current_path_expectation.rb', line 59

def self.autocorrect_incompatible_with
  [Style::TrailingCommaInArguments]
end

Instance Method Details

#as_is_matcher(node) ⇒ Object

Supported matchers: eq(…) / match(/regexp/) / match(‘regexp’)



46
47
48
49
50
# File 'lib/rubocop/cop/capybara/current_path_expectation.rb', line 46

def_node_matcher :as_is_matcher, <<~PATTERN
  (send
    #expectation_set_on_current_path ${:to :to_not :not_to}
    ${(send nil? :eq ...) (send nil? :match (regexp ...))})
PATTERN

#expectation_set_on_current_path(node) ⇒ Object



40
41
42
# File 'lib/rubocop/cop/capybara/current_path_expectation.rb', line 40

def_node_matcher :expectation_set_on_current_path, <<~PATTERN
  (send nil? :expect (send {(send nil? :page) nil?} :current_path))
PATTERN

#on_send(node) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/capybara/current_path_expectation.rb', line 63

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

#regexp_node_matcher(node) ⇒ Object



53
54
55
56
57
# File 'lib/rubocop/cop/capybara/current_path_expectation.rb', line 53

def_node_matcher :regexp_node_matcher, <<~PATTERN
  (send
    #expectation_set_on_current_path ${:to :to_not :not_to}
    $(send nil? :match ${str dstr xstr}))
PATTERN