Class: ConvenientService::RSpec::PrimitiveMatchers::Classes::DelegateTo::Entities::SubMatchers::Arguments::Commands::ApplyStubToTrackDelegations

Inherits:
Support::Command
  • Object
show all
Includes:
RSpec::Mocks::ExampleMethods
Defined in:
lib/convenient_service/rspec/primitive_matchers/classes/delegate_to/entities/sub_matchers/arguments/commands/apply_stub_to_track_delegations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(matcher:) ⇒ void



30
31
32
# File 'lib/convenient_service/rspec/primitive_matchers/classes/delegate_to/entities/sub_matchers/arguments/commands/apply_stub_to_track_delegations.rb', line 30

def initialize(matcher:)
  @matcher = matcher
end

Instance Attribute Details

#matcherConvenientService::RSpec::PrimitiveMatchers::Classes::DelegateTo (readonly)



24
25
26
# File 'lib/convenient_service/rspec/primitive_matchers/classes/delegate_to/entities/sub_matchers/arguments/commands/apply_stub_to_track_delegations.rb', line 24

def matcher
  @matcher
end

Instance Method Details

#callObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/convenient_service/rspec/primitive_matchers/classes/delegate_to/entities/sub_matchers/arguments/commands/apply_stub_to_track_delegations.rb', line 34

def call
  ##
  # NOTE: RSpec `allow(object).to receive(method).with(*args, **kwargs)` does NOT support block.
  # https://github.com/rspec/rspec-mocks/issues/1182#issuecomment-679820352
  #
  # NOTE: RSpec `allow(object).to receive(method) do` does NOT support `and_call_original`.
  # https://github.com/rspec/rspec-mocks/issues/774#issuecomment-54245277
  #
  # NOTE: That is why `and_wrap_original` is used.
  # https://relishapp.com/rspec/rspec-mocks/docs/configuring-responses/wrapping-the-original-implementation
  #
  # TODO: Check whether `matcher.object.frozen?` in order to show more explanatory exception instead of the following (provide an example of how to avoid it in the exception message):
  #   ArgumentError:
  #     Cannot proxy frozen objects, rspec-mocks relies on proxies for method stubbing and expectations
  #
  allow(matcher.inputs.object).to receive(matcher.inputs.method).and_wrap_original do |original, *actual_args, **actual_kwargs, &actual_block|
    ##
    # TODO: Add backtrace for easier reason tracing.
    #
    matcher.outputs.delegations << Entities::Delegation.new(
      object: matcher.inputs.object,
      method: matcher.inputs.method,
      args: actual_args,
      kwargs: actual_kwargs,
      block: actual_block
    )

    ##
    # NOTE: Imitates `and_call_original`.
    # TODO: What to do when `original.call` raises an exception?
    #
    original.call(*actual_args, **actual_kwargs, &actual_block) if matcher.inputs.should_call_original?
  end
end