Sha256: acf9a9d38298fca0fa99286b5bc8575839f8070ae7c7515a0a3c25b886efb3e8

Contents?: true

Size: 1.33 KB

Versions: 20

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks unreferenced `let!` calls being used for test setup.
      #
      # @example
      #   # Bad
      #   let!(:my_widget) { create(:widget) }
      #
      #   it 'counts widgets' do
      #     expect(Widget.count).to eq(1)
      #   end
      #
      #   # Good
      #   it 'counts widgets' do
      #     create(:widget)
      #     expect(Widget.count).to eq(1)
      #   end
      #
      #   # Good
      #   before { create(:widget) }
      #
      #   it 'counts widgets' do
      #     expect(Widget.count).to eq(1)
      #   end
      class LetSetup < Cop
        include RuboCop::RSpec::TopLevelDescribe

        MSG = 'Do not use `let!` for test setup.'.freeze

        def_node_search :let_bang, <<-PATTERN
          (block $(send nil? :let! (sym $_)) args ...)
        PATTERN

        def_node_search :method_called?, '(send nil? %)'

        def on_block(node)
          return unless example_group?(node)

          unused_let_bang(node) do |let|
            add_offense(let, location: :expression)
          end
        end

        private

        def unused_let_bang(node)
          let_bang(node) do |method_send, method_name|
            yield(method_send) unless method_called?(node, method_name)
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
rubocop-rspec-1.32.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.31.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.21.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.20.1 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.20.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.19.0 lib/rubocop/cop/rspec/let_setup.rb