summaryrefslogtreecommitdiff
path: root/spec/support/patches/rspec_mocks_prepended_methods.rb
blob: fa3a74c670cf8366dc44456147cfa43f6abbb4c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

# This patch allows stubbing of prepended methods
# Based on https://github.com/rspec/rspec-mocks/pull/1218

module RSpec
  module Mocks
    module InstanceMethodStasherForPrependedMethods
      private

      def method_owned_by_klass?
        owner = @klass.instance_method(@method).owner
        owner = owner.class unless Module === owner

        owner == @klass ||
          # When `extend self` is used, and not under any instance of
          (owner.singleton_class == @klass && !Mocks.space.any_instance_recorder_for(owner, true)) ||
          !method_defined_on_klass?(owner)
      end
    end
  end
end

module RSpec
  module Mocks
    module MethodDoubleForPrependedMethods
      def restore_original_method
        return show_frozen_warning if object_singleton_class.frozen?
        return unless @method_is_proxied

        remove_method_from_definition_target

        if @method_stasher.method_is_stashed?
          @method_stasher.restore
          restore_original_visibility
        end

        @method_is_proxied = false
      end

      def restore_original_visibility
        method_owner.__send__(@original_visibility, @method_name)
      end

      private

      def method_owner
        @method_owner ||= Object.instance_method(:method).bind(object).call(@method_name).owner
      end
    end
  end
end

RSpec::Mocks::InstanceMethodStasher.prepend(RSpec::Mocks::InstanceMethodStasherForPrependedMethods)
RSpec::Mocks::MethodDouble.prepend(RSpec::Mocks::MethodDoubleForPrependedMethods)