Module: Trailblazer::Test::Assertion::AssertFail

Extended by:
Trailblazer::Test::Assertion::AssertPass::Utils
Defined in:
lib/trailblazer/test/assertion/assert_fail.rb

Class Method Summary collapse

Methods included from Trailblazer::Test::Assertion::AssertPass::Utils

assert_after_call

Class Method Details

.arguments_for_assert_contract_errors(signal, ctx, contract_name:, expected_errors:) ⇒ Object

Raises:

  • (ExpectedErrorsTypeError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/trailblazer/test/assertion/assert_fail.rb', line 36

def arguments_for_assert_contract_errors(signal, ctx, contract_name:, expected_errors:, **)
  with_messages = expected_errors.is_a?(Hash)

  raise ExpectedErrorsTypeError, "expected_errors has to be an Array or Hash" unless expected_errors.is_a?(Array) || with_messages # TODO: test me!

  errors = ctx[:"contract.#{contract_name}"].errors.messages # TODO: this will soon change with the operation Errors object.

  if with_messages
    expected_errors = expected_errors.collect { |k, v| [k, Array(v)] }.to_h

    return expected_errors, errors
  else
    return expected_errors.sort, errors.keys.sort
  end
end

.arguments_for_assert_fail(signal) ⇒ Object



32
33
34
# File 'lib/trailblazer/test/assertion/assert_fail.rb', line 32

def arguments_for_assert_fail(signal)
  return false, Assertion::SUCCESS_TERMINI.include?(signal.to_h[:semantic]) # FIXME: same logic as in {#assert_pass}.
end

.assert_fail_with_model(signal, ctx, test:, **options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/trailblazer/test/assertion/assert_fail.rb', line 17

def assert_fail_with_model(signal, ctx, test:, **options)
  assert_after_call(ctx, **options) do |ctx|

    test.assert_equal *arguments_for_assert_fail(signal), error_message_for_assert_fail_after_call(signal, ctx, **options)

    if options[:expected_errors]
      # TODO: allow error messages from somewhere else.
      # only test _if_ errors are present, not the content.
      colored_errors = AssertPass::Errors.colored_errors_for(ctx)

      test.assert_equal *arguments_for_assert_contract_errors(signal, ctx, contract_name: :default, **options), "Actual contract errors: #{colored_errors}"
    end
  end
end

.call(activity, ctx, expected_errors = nil, test:, invoke:, **kws) ⇒ Object

expected_errors can be nil when using the Trailblazer::Test::Assertion#assert_fail block syntax.



10
11
12
13
14
# File 'lib/trailblazer/test/assertion/assert_fail.rb', line 10

def call(activity, ctx, expected_errors=nil, test:, invoke:, **kws)
  signal, ctx, _ = invoke.(activity, ctx)

  assert_fail_with_model(signal, ctx, expected_errors: expected_errors, test: test, operation: activity, **kws)
end

.error_message_for_assert_fail_after_call(signal, ctx, operation:) ⇒ Object



52
53
54
# File 'lib/trailblazer/test/assertion/assert_fail.rb', line 52

def error_message_for_assert_fail_after_call(signal, ctx, operation:, **)
  %{{#{operation}} didn't fail, it passed}
end