Class: RuboCop::Cop::RSpec::FilePath

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

Overview

Checks that spec file paths are consistent and well-formed.

By default, this checks that spec file paths are consistent with the test subject and and enforces that it reflects the described class/module and its optionally called out method.

With the configuration option ‘IgnoreMethods` the called out method will be ignored when determining the enforced path.

With the configuration option ‘CustomTransform` modules or classes can be specified that should not as usual be transformed from CamelCase to snake_case (e.g. ’RuboCop’ => ‘rubocop’ ).

With the configuration option ‘SpecSuffixOnly` test files will only be checked to ensure they end in ’_spec.rb’. This option disables checking for consistency in the test subject or test methods.

Examples:

# bad
whatever_spec.rb         # describe MyClass

# bad
my_class_spec.rb         # describe MyClass, '#method'

# good
my_class_spec.rb         # describe MyClass

# good
my_class_method_spec.rb  # describe MyClass, '#method'

# good
my_class/method_spec.rb  # describe MyClass, '#method'

when configuration is ‘IgnoreMethods: true`

# bad
whatever_spec.rb         # describe MyClass

# good
my_class_spec.rb         # describe MyClass

# good
my_class_spec.rb         # describe MyClass, '#method'

when configuration is ‘SpecSuffixOnly: true`

# good
whatever_spec.rb         # describe MyClass

# good
my_class_spec.rb         # describe MyClass

# good
my_class_spec.rb         # describe MyClass, '#method'

Constant Summary collapse

MSG =
'Spec path should end with `%<suffix>s`.'

Instance Method Summary collapse

Methods included from TopLevelGroup

#on_new_investigation, #top_level_groups

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Instance Method Details

#on_top_level_example_group(node) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/rubocop/cop/rspec/file_path.rb', line 72

def on_top_level_example_group(node)
  return unless top_level_groups.one?

  const_described(node) do |send_node, described_class, arguments|
    next if routing_spec?(arguments)

    ensure_correct_file_path(send_node, described_class, arguments)
  end
end