Class: RuboCop::Cop::ViewComponent::TestRenderedOutput

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/view_component/test_rendered_output.rb

Overview

Ensures that ViewComponent tests use ‘render_inline` to test rendered output rather than testing component methods directly.

This cop is only enabled for test files by default (see config).

Examples:

# bad
def test_formatted_title
  component = TitleComponent.new("hello")
  assert_equal "HELLO", component.formatted_title
end

# good
def test_formatted_title
  render_inline TitleComponent.new("hello")
  assert_text "HELLO"
end

Constant Summary collapse

MSG =
"Test instantiates a component but doesn't use `render_inline` or `render_preview`. " \
"Test the rendered output instead of component methods directly."

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object

Check RSpec-style it blocks



40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/view_component/test_rendered_output.rb', line 40

def on_block(node)
  return unless within_test_paths?
  return unless rspec_it_block?(node)
  return unless instantiates_component?(node)
  return if contains_render_method?(node)

  add_offense(node)
end

#on_def(node) ⇒ Object

Check Minitest-style test methods



29
30
31
32
33
34
35
36
37
# File 'lib/rubocop/cop/view_component/test_rendered_output.rb', line 29

def on_def(node)
  return unless within_test_paths?
  method_name = node.method_name.to_s
  return unless method_name.start_with?("test_")
  return unless instantiates_component?(node)
  return if contains_render_method?(node)

  add_offense(node)
end