Class: RuboCop::Cop::RSpec::UnexpectedRequires

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

Overview

Checks that rspec files do not contain requires.

As this can lead to unexpected behavior later when the code is not used with rspec.

Require the necessary files in the projects config or where you need it instead.

Examples:

# bad
require "lib/use_cases/my_work"

describe UseCases::MyWork do

end

# good
describe UseCases::MyWork do

end

Constant Summary collapse

MSG =
'Do not require anything in a test file.'
IGNORED_PATTERN =
/(spec|rails)_helper/

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject



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

def on_new_investigation
  @ignore_file = processed_source.path.match?(IGNORED_PATTERN)
end

#on_send(node) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/cop/rspec/unexpected_requires.rb', line 40

def on_send(node)
  return if @ignore_file

  require_symbol?(node) do |match|
    next if match.respond_to?(:value) &&
      match.value.match?(IGNORED_PATTERN)

    add_offense(node, message: MSG)
  end
end