Skip to content

Commit

Permalink
more block and expected_errors tests for assert_fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Dec 6, 2024
1 parent 4a87b98 commit 40d7c6a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
1 change: 0 additions & 1 deletion lib/trailblazer/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ module Test
require "trailblazer/test/assertion/assert_fail"
require "trailblazer/test/assertion"
require "trailblazer/test/operation/helper"
# require "trailblazer/test/operation/assertions"
require "trailblazer/test/assertion/suite"
# require "trailblazer/test/operation/policy_assertions"
42 changes: 36 additions & 6 deletions test/assertion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def validate(ctx, params:, **)
test_2 = test.new(:test_0002_anonymous)
failures = test_2.()
assert_equal 1, failures.size
failures[0].inspect.must_equal %(#<Minitest::Assertion: Property [id] mismatch.
assert_equal failures[0].inspect, %(#<Minitest::Assertion: Property [id] mismatch.
Expected: 2
Actual: 1>)
end
Expand Down Expand Up @@ -119,7 +119,7 @@ def validate(ctx, params:, **)
test_1 = test.new(:test_0001_anonymous)
failures = test_1.()
assert_equal failures.size, 1
failures[0].inspect.must_equal %(#<Minitest::Assertion: Property [id] mismatch.
assert_equal failures[0].inspect, %(#<Minitest::Assertion: Property [id] mismatch.
Expected: 1
Actual: nil>)

Expand All @@ -135,7 +135,7 @@ def validate(ctx, params:, **)
test_4 = test.new(:test_0004_anonymous)
failures = test_4.()
assert_equal failures.size, 1
failures[0].inspect.must_equal %(#<Minitest::Assertion: --- expected
assert_equal failures[0].inspect, %(#<Minitest::Assertion: --- expected
+++ actual
@@ -1 +1 @@
-AssertionsTest::Record(keyword_init: true)
Expand Down Expand Up @@ -194,6 +194,23 @@ def validate(ctx, params:, **)
@_m = true
end
end

# test_0007_anonymous
# both expected_errors and block are considered.
it do
assert_fail Update, {params: {title: nil}}, [:title] do |result|
assert_equal result[:"contract.default"].errors.messages, {:title=>["is missing"]}
@_m = true
end
end

# test_0008_anonymous
# expected_errors is wrong
it do
assert_fail Update, {params: {title: nil}}, [:title_XXX] do |result|
@_m = true
end
end
end

test_1 = test.new(:test_0001_anonymous)
Expand All @@ -207,14 +224,14 @@ def validate(ctx, params:, **)
test_3 = test.new(:test_0003_anonymous)
failures = test_3.()
assert_equal failures.size, 1
failures[0].inspect.must_equal %(#<Minitest::Assertion: {AssertionsTest::Update} didn't fail, it passed.
assert_equal failures[0].inspect, %(#<Minitest::Assertion: {AssertionsTest::Update} didn't fail, it passed.
Expected: false
Actual: true>)

test_4 = test.new(:test_0004_anonymous)
failures = test_4.()
assert_equal failures.size, 1
failures[0].inspect.must_equal %(#<Minitest::Assertion: Actual contract errors: \e[33m{:title=>[\"is missing\"]}\e[0m.
assert_equal failures[0].inspect, %(#<Minitest::Assertion: Actual contract errors: \e[33m{:title=>[\"is missing\"]}\e[0m.
Expected: {:title=>[\"is XXX\"]}
Actual: {:title=>[\"is missing\"]}>)

Expand All @@ -227,8 +244,21 @@ def validate(ctx, params:, **)
failures = test_6.()
assert_nil test_6.instance_variable_get(:@_m) # block is not executed.
assert_equal failures.size, 1
failures[0].inspect.must_equal %(#<Minitest::Assertion: {AssertionsTest::Update} didn't fail, it passed.
assert_equal failures[0].inspect, %(#<Minitest::Assertion: {AssertionsTest::Update} didn't fail, it passed.
Expected: false
Actual: true>)

test_7 = test.new(:test_0007_anonymous)
failures = test_7.()
assert_equal test_7.instance_variable_get(:@_m), true
assert_equal failures.size, 0

test_8 = test.new(:test_0008_anonymous)
failures = test_8.()
assert_nil test_8.instance_variable_get(:@_m) # block is not executed.
assert_equal failures.size, 1
assert_equal failures[0].inspect, %(#<Minitest::Assertion: Actual contract errors: \e[33m{:title=>[\"is missing\"]}\e[0m.
Expected: [:title_XXX]
Actual: [:title]>)
end
end

0 comments on commit 40d7c6a

Please sign in to comment.