Sha256: 1b9fa180de87dda695af1bb9f6a7cf8d556129d16d5a06533ab88c275a4e4539

Contents?: true

Size: 1.05 KB

Versions: 12

Compression:

Stored size: 1.05 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 < Cop
        MSG = 'Do not use `%<expect>s` in `%<hook>s` hook'

        def_node_search :expectation, Expectations::ALL.send_pattern

        def on_block(node)
          return unless hook?(node)
          return if node.body.nil?

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

        private

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

Version data entries

12 entries across 12 versions & 1 rubygems

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