Skip to content

Commit

Permalink
test all Activity logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Dec 12, 2024
1 parent 57fce9d commit 8290758
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
4 changes: 3 additions & 1 deletion lib/trailblazer/test/assertion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def self.invoke_activity(ctx, operation:, **)
end

def self.invoke_activity_with_tracing(ctx, operation:, **)
raise
signal, (ctx, _) = Developer::Wtf.invoke(operation, [ctx, {}])

return signal, ctx
end

module Assert
Expand Down
2 changes: 1 addition & 1 deletion lib/trailblazer/test/assertion/assert_fail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def arguments_for_assert_contract_errors(signal, ctx, contract_name:, expected_e

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.
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
Expand Down
47 changes: 32 additions & 15 deletions test/assertion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@ class Create < Trailblazer::Activity::FastTrack
step :model

def validate(ctx, params:, **)
params[:title]
return true if params[:title]

ctx[:"contract.default"] = Struct.new(:errors).new(Struct.new(:messages).new({:title => ["is missing"]}))
false
end

def model(ctx, params:, **)
Expand All @@ -357,23 +360,16 @@ def model(ctx, params:, **)
assert_pass Create, {params: {title: "Roxanne"}},
title: "Roxanne"
end

it do
assert_fail Create, {params: {}}, [:title]
end
end

# Test with the Assertion::Suite "DSL" module.
class AssertionActivitySuiteTest < Minitest::Spec
Record = AssertionsTest::Record

class Create < Trailblazer::Activity::FastTrack
step :validate
step :model

def validate(ctx, params:, **)
params[:title]
end

def model(ctx, params:, **)
ctx[:model] = Record.new(**params)
end
end
Create = AssertionActivityTest::Create

include Trailblazer::Test::Assertion::Suite
include Trailblazer::Test::Assertion::Activity::Assert
Expand All @@ -382,6 +378,27 @@ def model(ctx, params:, **)
let(:operation) { Create }

it do
assert_pass?({params: {title: "Roxanne"}}, {title: "Roxanne"})
out, _ = capture_io do
assert_pass?({title: "Roxanne"}, {title: "Roxanne"})
end

assert_equal out, %(AssertionActivityTest::Create
|-- \e[32mStart.default\e[0m
|-- \e[32mvalidate\e[0m
|-- \e[32mmodel\e[0m
`-- End.success
)
end

it "{#assert_fail} with wtf?" do
out, _ = capture_io do
assert_fail?({title: nil}, [:title])
end

assert_equal out, %(AssertionActivityTest::Create
|-- \e[32mStart.default\e[0m
|-- \e[33mvalidate\e[0m
`-- End.failure
)
end
end

0 comments on commit 8290758

Please sign in to comment.