Class: RuboCop::Cop::RSpec::StubProducts

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/stub_products.rb

Overview

Suggest to use stub_products instead of veil/unveil_product which queries the database and can cause flaky tests.

# bad
unveil_product('MY_PRODUCT')
veil_product('MY_PRODUCT')

# good
stub_products('MY_PRODUCT' => true)
stub_products('MY_PRODUCT' => false)
stub_products('MY_PRODUCT' => group)
stub_products('MY_PRODUCT' => [group1, group2])
stub_products(MY_PRODUCT: true)

Constant Summary collapse

MSG =
'Use `stub_products` instead of veil/unveil_product.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/rspec/stub_products.rb', line 33

def on_send(node)
  return unless veil_product?(node) || unveil_product?(node)

  add_offense(node) do |corrector|
    if (match = /^\S*\s+(\S+)|\(([^)]+)\)/.match(node.source))
      match1, match2 = match.captures
      product_code = match1 || match2

      product_is_available = !veil_product?(node)
      subst = "stub_products(#{product_code} => #{product_is_available})"

      corrector.replace(node, subst)
    end
  end
end