Class: RSpock::AST::InteractionToMochaMockTransformation

Inherits:
ASTTransform::AbstractTransformation
  • Object
show all
Defined in:
lib/rspock/ast/interaction_to_mocha_mock_transformation.rb

Overview

Transforms an :rspock_interaction node into Mocha mock setup code.

Input: s(:rspock_interaction, cardinality, receiver, sym, args, outcome, block_pass) Output: receiver.expects(:message).with(*args).times(n).returns(value)

The outcome node type maps directly to the Mocha chain method:

:rspock_stub_returns -> .returns(value)
:rspock_stub_raises  -> .raises(exception_class, ...)

When block_pass is present, wraps the expects chain with a BlockCapture.capture call.

Constant Summary collapse

OUTCOME_METHODS =
{
  rspock_stub_returns: :returns,
  rspock_stub_raises: :raises,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(index = 0) ⇒ InteractionToMochaMockTransformation

Returns a new instance of InteractionToMochaMockTransformation.



23
24
25
# File 'lib/rspock/ast/interaction_to_mocha_mock_transformation.rb', line 23

def initialize(index = 0)
  @index = index
end

Instance Method Details

#run(interaction) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rspock/ast/interaction_to_mocha_mock_transformation.rb', line 27

def run(interaction)
  return interaction unless interaction.type == :rspock_interaction

  result = chain_call(interaction.receiver, :expects, s(:sym, interaction.message))
  result = chain_call(result, :with, *interaction.args.children) if interaction.args
  result = build_cardinality(result, interaction.cardinality)
  if interaction.outcome
    result = chain_call(result, OUTCOME_METHODS.fetch(interaction.outcome.type), *interaction.outcome.children)
  end

  if interaction.block_pass
    build_block_capture_setup(result, interaction.receiver, interaction.message)
  else
    result
  end
end