Class: RuboCop::Cop::Sevencop::RSpecRailsHaveHttpStatus

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sevencop/rspec_rails_have_http_status.rb

Overview

Always check status code with ‘have_http_status`.

Examples:

# bad
it 'creates a new post' do
  expect { subject }.to change(Post, :count).by(1)
end

# good
it 'creates a new post' do
  expect { subject }.to change(Post, :count).by(1)
  expect(response).to have_http_status(200)
end

# good
it 'creates a new post' do
  expect { subject }.to change(Post, :count).by(1)
  expect(response).to redirect_to(posts_path)
end

Constant Summary collapse

MSG =
'Always check status code with `have_http_status`.'
EXAMPLE_METHOD_NAMES =
%i[
  example
  it
  specify
].to_set.freeze
STATUS_CHECK_METHOD_NAMES =
%i[
  have_http_status
  redirect_to
].to_set.freeze

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::BlockNode)


41
42
43
44
45
46
# File 'lib/rubocop/cop/sevencop/rspec_rails_have_http_status.rb', line 41

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  return unless example_method?(node)
  return if including_status_check_method?(node)

  add_offense(node)
end