Sha256: 15208e069d8f4a7b5ff88a0b139eef5816d5ae99fecfc4e06e731ba9557c7235

Contents?: true

Size: 1.12 KB

Versions: 13

Compression:

Stored size: 1.12 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)
          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

13 entries across 13 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/expect_in_hook.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/expect_in_hook.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/expect_in_hook.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/expect_in_hook.rb