Sha256: 44281d6aa775a17173ac1e0662be0c1f13392e1f51d1cfc612e2184831fff3d6

Contents?: true

Size: 1.17 KB

Versions: 14

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_pattern('#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

14 entries across 14 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/expect_in_hook.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.18.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.18.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.17.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.17.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.16.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.15.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.14.2 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.14.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.14.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.13.2 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.13.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.13.0 lib/rubocop/cop/rspec/expect_in_hook.rb