Module: Trailblazer::Test::Operation::Helper

Defined in:
lib/trailblazer/test/operation/helper.rb

Defined Under Namespace

Classes: OperationFailedError

Instance Method Summary collapse

Instance Method Details

#call(operation_class, **args, &block) ⇒ Object



3
4
5
# File 'lib/trailblazer/test/operation/helper.rb', line 3

def call(operation_class, **args, &block)
  call!(operation_class, args, &block)
end

#call!(operation_class, raise_on_failure: false, **args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/trailblazer/test/operation/helper.rb', line 28

def call!(operation_class, raise_on_failure: false, **args)
  operation_class.trace(**args).tap do |result|
    unless result.success?

      msg = "factory(#{operation_class}) has failed"

      unless result["contract.default"].nil? # should we allow to change contract name?
        if result["contract.default"].errors.messages.any?
          msg += " due to validation errors: #{result["contract.default"].errors.messages}"
        end
      end

      if raise_on_failure
        result.wtf?
        raise OperationFailedError, msg
      end
    end

    yield result if block_given?

    result
  end
end

#factory(operation_class, **args, &block) ⇒ Object



7
8
9
# File 'lib/trailblazer/test/operation/helper.rb', line 7

def factory(operation_class, **args, &block)
  call!(operation_class, args.merge(raise_on_failure: true), &block)
end

#mock_step(operation_class, id:, subprocess: nil, subprocess_path: nil) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/trailblazer/test/operation/helper.rb', line 11

def mock_step(operation_class, id:, subprocess: nil, subprocess_path: nil)
  raise ArgumentError, "Missing block: `mock_step` requires a block." unless block_given?

  block = ->(ctx, **) { yield(ctx) }

  # If deeply nested step needs to be mocked, use the `patch` provided by Subprocess
  if subprocess
    return Class.new(operation_class) do
      patch = { subprocess_path => ->() { step block, replace: id, id: id } }
      step Subprocess(subprocess, patch: patch), replace: subprocess, id: subprocess
    end
  end

  Class.new(operation_class) { step block, replace: id, id: id }
end