Class: GLRubocop::GLCops::ConsolidateRequestSystemSpecs
- Inherits:
-
RuboCop::Cop::Base
- Object
- RuboCop::Cop::Base
- GLRubocop::GLCops::ConsolidateRequestSystemSpecs
- Defined in:
- lib/gl_rubocop/gl_cops/consolidate_request_system_specs.rb
Overview
This cop ensures request and system specs consolidate examples in a single it block,
per each describe and context.
Reason: Setup for specs should go in let/before blocks - which are different for each context.
it blocks with the same setup should be consolidated to keep our tests fast.
Good:
RSpec.describe UsersController, type: :request do
describe 'GET /users' do
it 'returns users' do
get users_path
expect(response).to be_successful
expect(response).to render_template(:index)
get users_path, headers: {format: :json}
expect(response).to be_successful
expect(response.content_type).to eq('application/json')
end
end
describe 'GET /users/id' do
let(:user_id) { user.id }
it 'returns user' do
get user_path(user_id)
expect(response).to be_successful
end
context 'with unknown user' do
let(:user_id) { 111111 }
it 'does not return user' do
get user_path(user_id)
expect(response).not_to be_successful
end
end
end
end
Bad:
RSpec.describe UsersController, type: :request do
describe 'GET /users' do
it 'returns success' do
get users_path
expect(response).to be_successful
end
it 'returns json' do
get users_path
expect(response.content_type).to eq('application/json')
end
end
end
Constant Summary collapse
- MSG =
'Consolidate examples with the same setup in request specs and system specs. ' \ 'Use a single it block instead of multiple it blocks.'
- RSPEC_EXAMPLE_METHODS =
%i[it specify example].freeze
Instance Method Summary collapse
- #on_block(node) ⇒ Object
- #on_new_investigation ⇒ Object
- #request_or_system_type?(node) ⇒ Object
- #rspec_group?(node) ⇒ Object
Instance Method Details
#on_block(node) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/gl_rubocop/gl_cops/consolidate_request_system_specs.rb', line 74 def on_block(node) return unless rspec_group?(node) return unless request_or_system_spec?(node) check_multiple_examples(node) end |
#on_new_investigation ⇒ Object
70 71 72 |
# File 'lib/gl_rubocop/gl_cops/consolidate_request_system_specs.rb', line 70 def on_new_investigation @spec_type_cache = {} end |
#request_or_system_type?(node) ⇒ Object
66 67 68 |
# File 'lib/gl_rubocop/gl_cops/consolidate_request_system_specs.rb', line 66 def_node_matcher :request_or_system_type?, <<~PATTERN (block (send _ _ ... (hash <(pair (sym :type) (sym {:request :system})) ...>)) ...) PATTERN |
#rspec_group?(node) ⇒ Object
61 62 63 |
# File 'lib/gl_rubocop/gl_cops/consolidate_request_system_specs.rb', line 61 def_node_matcher :rspec_group?, <<~PATTERN (block (send _ {:describe :context} ...) ...) PATTERN |