Class: Rubocop::Cop::RSpec::RailsControllerTesting

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/rails_controller_testing.rb

Overview

Flags the use of ‘assigns` and `render_template` in controller and request specs.

Examples:

# bad
ssh_key = create(:personal_key, user: user)
get admin_credentials_path(filter: 'ssh_keys')
expect(assigns(:credentials)).to match_array([ssh_key])

# good
ssh_keys = create_list(:personal_key, user: user)
get admin_credentials_path(filter: 'ssh_keys')
expect(response.body).to include(ssh_key.title)

# bad
expect(response).to render_template :show

# good
expect(response.body).to include('Cool stuff')

See Also:

Constant Summary collapse

MSG_TEMPLATE =
"Avoid using `%{name}`. Instead, assert on observable outputs such as the response body. See https://docs.gitlab.com/ee/development/testing_guide/testing_levels.html#about-controller-tests"
RESTRICT_ON_SEND =
%i[assigns render_template].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



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

def on_send(node)
  add_offense(node, message: format(MSG_TEMPLATE, name: node.method_name))
end