Sha256: fee858fde76ce1e28f52a0747861b35200f2c122f74e886ec8df011f4cd8e3d1

Contents?: true

Size: 1.08 KB

Versions: 16

Compression:

Stored size: 1.08 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_pattern('#Expectations.all')

        def on_block(node)
          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

        private

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

Version data entries

16 entries across 14 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/expect_in_hook.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/expect_in_hook.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/expect_in_hook.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.12.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.12.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.11.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.11.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.10.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.9.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.8.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.7.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.6.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.5.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.4.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.3.0 lib/rubocop/cop/rspec/expect_in_hook.rb