Class: RuboCop::Cop::Obsession::Rspec::DescribePublicMethod

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

Overview

This cop checks for ‘describe` blocks that test private methods.

If you are doing black box unit testing, it means that you are only interested in testing external behavior, a.k.a public interface, a.k.a public methods. Private methods are considered implementation details and are not directly tested.

If you need to test a Rails callback, test it indirectly through its corresponding Rails public method, e.g. #create, #save, etc.

Examples:


class Comment < ApplicationRecord
  after_create_commit :notify_users

  private

  def notify_users
    ...
  end
end

# bad
describe '#notify_users' do
  ...
end

# good
describe '#create' do
  it 'notifies users' do
    ...
  end
end

Constant Summary collapse

MSG =
'Only test public methods.'

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



55
56
57
58
59
60
# File 'lib/rubocop/cop/obsession/rspec/describe_public_method.rb', line 55

def on_block(node)
  on_context_method(node) do |method_name|
    method_name = method_name.sub('#', '').to_sym
    add_offense(node) if private_methods.include?(method_name)
  end
end