Sha256: 70b47e0c788c0c3499c4ff3c4d901a1cb65cbe8072b7beb1c1b2a4a523211621

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      module Capybara
        # Checks for consistent method usage in feature specs.
        #
        # @example
        #   # bad
        #   feature 'User logs in' do
        #     given(:user) { User.new }
        #
        #     background do
        #       visit new_session_path
        #     end
        #
        #     scenario 'with OAuth' do
        #       # ...
        #     end
        #   end
        #
        #   # good
        #   describe 'User logs in' do
        #     let(:user) { User.new }
        #
        #     before do
        #       visit new_session_path
        #     end
        #
        #     it 'with OAuth' do
        #       # ...
        #     end
        #   end
        class FeatureMethods < Cop
          MSG = 'Use `%s` instead of `%s`.'.freeze

          # https://git.io/v7Kwr
          MAP = {
            background: :before,
            scenario:   :it,
            xscenario:  :xit,
            given:      :let,
            given!:     :let!,
            feature:    :describe
          }.freeze

          def_node_matcher :feature_method?, <<-PATTERN
            (send {(const nil :RSpec) nil} ${:#{MAP.keys.join(' :')}} ...)
          PATTERN

          def on_send(node)
            feature_method?(node) do |match|
              add_offense(node, :selector, format(MSG, MAP[match], match))
            end
          end

          def autocorrect(node)
            lambda do |corrector|
              corrector.replace(node.loc.selector, MAP[node.method_name].to_s)
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-rspec-1.17.1 lib/rubocop/cop/rspec/capybara/feature_methods.rb
rubocop-rspec-1.17.0 lib/rubocop/cop/rspec/capybara/feature_methods.rb