Sha256: 55fef51b1129aa8b1048cae24da0cdb126f62309e2818c3f7ac1706cecca3fae

Contents?: true

Size: 1.28 KB

Versions: 10

Compression:

Stored size: 1.28 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
        MSG = 'Do not use `let!` to setup objects not referenced in tests.'

        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)
          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

10 entries across 10 versions & 1 rubygems

Version Path
rubocop-rspec-1.40.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.39.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.38.1 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.38.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.37.1 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.37.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.36.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.35.0 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.34.1 lib/rubocop/cop/rspec/let_setup.rb
rubocop-rspec-1.34.0 lib/rubocop/cop/rspec/let_setup.rb