Class: RSpec::Mocks::Matchers::Receive

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/armour/receive_patch.rb

Overview

This class is patched to prevent mocking ActiveRecord associations and finders.

Class Method Summary collapse

Class Method Details

.patch_for_armour!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rspec/armour/receive_patch.rb', line 8

def self.patch_for_armour!
  # rubocop:disable ThreadSafety/ClassInstanceVariable
  @armour_patched ||= false
  return if @armour_patched

  @armour_patched = true
  # rubocop:enable ThreadSafety/ClassInstanceVariable

  %i[
    setup_expectation
    setup_negative_expectation
    setup_allowance
    setup_any_instance_expectation
    setup_any_instance_negative_expectation
    setup_any_instance_allowance
    matches?
    does_not_match?
  ].each do |method|
    next unless method_defined?(method)

    patch_method(method)
  end
end

.patch_method(method) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rspec/armour/receive_patch.rb', line 32

def self.patch_method(method)
  original_method = instance_method(method)
  define_method(method) do |subject, &block|
    if ENV["RSPEC_ARMOUR_DEBUG"] == "true"
      puts "Checking RSpec::Armour '#{@message}' on #{subject}"
    end
    if RSpec::Armour::Checker.restricted?(subject, @message)
      message = "Mocking/stubbing ActiveRecord association or finder `#{@message}` " \
                "on `#{subject.inspect}` is restricted by rspec-armour."
      raise RSpec::Armour::MockError, message
    end
    original_method.bind_call(self, subject, &block)
  end
end