Class: RuboCop::Cop::Ezcater::RspecRequireBrowserMock

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/ezcater/rspec_require_browser_mock.rb

Overview

Enforce use of ‘mock_ezcater_app`, `mock_chrome_browser` & `mock_custom_browser` helpers instead of mocking `Browser` or `EzBrowser` directly.

Examples:


# good
mock_ezcater_app
mock_chrome_browser
mock_custom_browser
mock_ezcater_app(device: "My Device", version: "1.2.3", language: "en=US,en")
mock_chrome_browser(device: "My Device", version: "1.2.3", language: "en=US,en")
mock_custom_browser(user_agent: "My Custom Agent", language: "en=US,en")

# bad
allow(Browser).to receive...
allow(EzBrowser).to receive...

Constant Summary collapse

MSG =
"Use the mocks provided by `BrowserHelpers` instead of mocking `%<node_source>s`"

Instance Method Summary collapse

Instance Method Details

#on_const(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/ezcater/rspec_require_browser_mock.rb', line 29

def on_const(node)
  return unless browser_const?(node) && node.descendants.empty?

  # Walk to send node where method = :allow
  match_node = node
  match_node = match_node.parent while not_allow_send_node?(match_node.parent)
  match_node = match_node.parent

  # Validate send node, method = :allow; could have never been found
  return unless allow_send_node?(match_node)

  # Finish tree navigation to full line for highlighting
  match_node = match_node.parent while match_node.parent
  add_offense(match_node,
              location: :expression,
              message: format(MSG, node_source: node.source))
end