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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
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`'
RESTRICT_ON_SEND =
%i[expect].freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#as_is_matcher(node) ⇒ Object

Supported matchers: eq(…) / match(/regexp/) / match('regexp')


42
43
44
45
46
# File 'lib/rubocop/cop/rspec/capybara/current_path_expectation.rb', line 42

def_node_matcher :as_is_matcher, <<-PATTERN
  (send
    #expectation_set_on_current_path $#Runners.all
    ${(send nil? :eq ...) (send nil? :match (regexp ...))})
PATTERN

#expectation_set_on_current_path(node) ⇒ Object


36
37
38
# File 'lib/rubocop/cop/rspec/capybara/current_path_expectation.rb', line 36

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

#on_send(node) ⇒ Object


55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/rspec/capybara/current_path_expectation.rb', line 55

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_str_matcher(node) ⇒ Object


49
50
51
52
53
# File 'lib/rubocop/cop/rspec/capybara/current_path_expectation.rb', line 49

def_node_matcher :regexp_str_matcher, <<-PATTERN
  (send
    #expectation_set_on_current_path $#Runners.all
    $(send nil? :match (str $_)))
PATTERN