Class: RuboCop::Cop::Capybara::RSpec::HaveContent

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/capybara/rspec/have_content.rb

Overview

Checks for usage of ‘have_content` and `have_no_content`.

Capybara provides ‘have_text` and `have_no_text` matchers that are more concise and preferred over their aliases `have_content` and `have_no_content`.

Examples:

# bad
expect(page).to have_content('capy')
expect(page).to have_no_content('bara')

# good
expect(page).to have_text('capy')
expect(page).to have_no_text('bara')

Constant Summary collapse

MSG =
'Prefer `%<good>s` over `%<bad>s`.'
RESTRICT_ON_SEND =
%i[have_content have_no_content].freeze
PREFERRED_METHOD =
{
  'have_content' => 'have_text',
  'have_no_content' => 'have_no_text'
}.freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/capybara/rspec/have_content.rb', line 32

def on_send(node)
  method_node = node.loc.selector
  add_offense(method_node,
              message: message(method_node)) do |corrector|
    corrector.replace(method_node,
                      PREFERRED_METHOD[method_node.source])
  end
end