Sha256: 0ca7f923d806a8cacaf9e602240e00a8822bb62e993e479aa59b9a3b99434a2e
Contents?: true
Size: 1.72 KB
Versions: 58
Compression:
Stored size: 1.72 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Ezcater # Enforce use of `mock_feature_flag` helper instead of mocking `FeatureFlag.is_active?` directly. # # @example # # # good # mock_feature_flag("MyFeatureFlag", true) # mock_feature_flag("MyFeatureFlag", { user: current_user }, true) # # # bad # allow(FeatureFlag).to receive(:is_active?).and_return(true) # allow(FeatureFlag).to receive(:is_active?).with("MyFeatureFlag").and_return(true) # allow(FeatureFlag).to receive(:is_active?).with("MyFeatureFlag", user: current_user).and_return(true) class RspecRequireFeatureFlagMock < Cop MSG = "Use the `mock_feature_flag` helper instead of mocking `allow(FeatureFlag)`" def_node_matcher :feature_flag_const?, <<~PATTERN (const _ :FeatureFlag) PATTERN def on_const(node) return unless feature_flag_const?(node) # 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) end private def not_allow_send_node?(node) node && !allow_send_node?(node) end def allow_send_node?(node) node&.send_type? && node.method_name == :allow end end end end end
Version data entries
58 entries across 58 versions & 1 rubygems