Sha256: 05c33043aa295aec718df483f322dc223c34c2c404b554e6d7b7dfc95702bbc8

Contents?: true

Size: 1.17 KB

Versions: 26

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Do not use `expect` in hooks such as `before`.
      #
      # @example
      #   # bad
      #   before do
      #     expect(something).to eq 'foo'
      #   end
      #
      #   # bad
      #   after do
      #     expect_any_instance_of(Something).to receive(:foo)
      #   end
      #
      #   # good
      #   it do
      #     expect(something).to eq 'foo'
      #   end
      #
      class ExpectInHook < Base
        MSG = 'Do not use `%<expect>s` in `%<hook>s` hook'

        # @!method expectation(node)
        def_node_search :expectation, '(send nil? #Expectations.all ...)'

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          return unless hook?(node)
          return if node.body.nil?

          expectation(node.body) do |expect|
            add_offense(expect.loc.selector,
                        message: message(expect, node))
          end
        end

        alias on_numblock on_block

        private

        def message(expect, hook)
          format(MSG, expect: expect.method_name, hook: hook.method_name)
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 4 rubygems

Version Path
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.27.0 lib/rubocop/cop/rspec/expect_in_hook.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.26.1/lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.26.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.26.0 lib/rubocop/cop/rspec/expect_in_hook.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/expect_in_hook.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/expect_in_hook.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/expect_in_hook.rb