Class: RuboCop::Cop::Sevencop::RSpecRailsStatusCodeCheckBySubject

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/sevencop/rspec_rails_status_code_check_by_subject.rb

Overview

Use ‘expect(response).to have_http_status(code)` instead of `is_expected.to eq(code)`.

Examples:

# bad
is_expected.to eq(200)

# good
expect(response).to have_http_status(200)

# bad
is_expected.to eq(:ok)

# good
expect(response).to have_http_status(:ok)

Constant Summary collapse

MSG =
'Use `expect(response).to have_http_status(code)` instead of `is_expected.to eq(code)`.'
RESTRICT_ON_SEND =
%i[
  to
].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/sevencop/rspec_rails_status_code_check_by_subject.rb', line 31

def on_send(node)
  return unless is_expected_to_eq_code?(node)

  add_offense(node) do |corrector|
    corrector.insert_before(node, "subject\n")
    corrector.replace(node.receiver, 'expect(response)')
    corrector.replace(node.first_argument.location.selector, 'have_http_status')
  end
end