Sha256: 3696fbea2f1e0637d2c0255464d28aa3873afdc73e6a12875282be9507beae6b

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 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'

        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

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-rspec-2.2.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.1.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.0.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-2.0.0 lib/rubocop/cop/rspec/expect_in_hook.rb