Module: MockSuey::RSpec::ProxyMethodInvokedHook

Defined in:
lib/mock_suey/rspec/proxy_method_invoked.rb

Instance Method Summary collapse

Instance Method Details

#proxy_method_invoked(obj, *args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mock_suey/rspec/proxy_method_invoked.rb', line 19

def proxy_method_invoked(obj, *args, &block)
  return super if obj.is_a?(::RSpec::Mocks::TestDouble) && !obj.is_a?(::RSpec::Mocks::VerifyingDouble)

  receiver_class = @proxy.target_class
  method_name = @method_name

  # TODO: Make conversion customizable to support .perform_later -> #perform
  # and other similar use-cases
  if method_name == :new && receiver_class.singleton_class?
    receiver_class, method_name = receiver_class.instance_class, :initialize
  end

  method_call = MockSuey::MethodCall.new(
    receiver_class:,
    method_name:,
    arguments: args,
    metadata: {example: ::RSpec.current_example}
  )

  super.tap do |ret|
    method_call.return_value = ret
    MockSuey.handle_mocked_call(method_call)
  end
end