diff --git a/lib/trailblazer/test/assertion/assert_fail.rb b/lib/trailblazer/test/assertion/assert_fail.rb index 454a7ce..562b2dc 100644 --- a/lib/trailblazer/test/assertion/assert_fail.rb +++ b/lib/trailblazer/test/assertion/assert_fail.rb @@ -6,24 +6,26 @@ module AssertFail extend AssertPass::Utils - def call(activity, ctx, expected_errors, use_wtf=false, test:, **kws, &block) + # {expected_errors} can be nil when using the {#assert_fail} block syntax. + def call(activity, ctx, expected_errors=nil, test:, **kws, &block) result, ctx, _ = call_operation(ctx, operation: activity) # FIXME: remove kws? assert_fail_with_model(result, ctx, expected_errors: expected_errors, test: test, user_block: block, operation: activity) end # @private - def assert_fail_with_model(result, ctx, test:, **options, &block) + def assert_fail_with_model(result, ctx, test:, **options) assert_after_call(result, **options) do |result| test.assert_equal *arguments_for_assert_fail(result), error_message_for_assert_fail_after_call(result, **options) + if options[:expected_errors] + # TODO: allow error messages from somewhere else. + # only test _if_ errors are present, not the content. + colored_errors = colored_errors_for(result) - # TODO: allow error messages from somewhere else. - # only test _if_ errors are present, not the content. - colored_errors = colored_errors_for(result) - - test.assert_equal *arguments_for_assert_contract_errors(result, contract_name: :default, **options), "Actual contract errors: #{colored_errors}" + test.assert_equal *arguments_for_assert_contract_errors(result, contract_name: :default, **options), "Actual contract errors: #{colored_errors}" + end end end diff --git a/lib/trailblazer/test/assertion/assert_pass.rb b/lib/trailblazer/test/assertion/assert_pass.rb index 7e30606..18b9a00 100644 --- a/lib/trailblazer/test/assertion/assert_pass.rb +++ b/lib/trailblazer/test/assertion/assert_pass.rb @@ -43,6 +43,7 @@ def call_operation(ctx, operation:, invoke_method: :call, **) # @private def assert_after_call(result, user_block: raise, **kws) yield(result) + user_block.call(result) if user_block result @@ -59,7 +60,7 @@ def colored_errors_for(result) colored_errors = %{\e[33m#{errors}\e[0m} end - end + end # Utils extend Utils end diff --git a/lib/trailblazer/test/operation/assertions.rb b/lib/trailblazer/test/operation/assertions.rb index 65cb31d..fd08216 100644 --- a/lib/trailblazer/test/operation/assertions.rb +++ b/lib/trailblazer/test/operation/assertions.rb @@ -175,17 +175,7 @@ def expected_attributes_for(expected_attributes_to_merge, expected_attributes:, # operation.send(invoke_method, ctx) # end - # @private - class CtxHash < Hash - include Hashie::Extensions::DeepMerge - end - - # @private - def merge_for(dest, source, deep_merge) - return dest.merge(source) unless deep_merge - CtxHash[dest].deep_merge(CtxHash[source]) # FIXME: this subclasses ActiveRecord classes in dest like {class: ReportSubscription} - end # @private # def colored_errors_for(result) @@ -204,35 +194,7 @@ def merge_for(dest, source, deep_merge) # result[model_at] # end - def Ctx(merge_with_ctx={}, exclude: false, merge: true, **kws) - if merge - options = normalize_kws_for_ctx(**kws) - key_in_params = options[:key_in_params] - default_ctx = options[:default_ctx] - - # Extract {:params} from {default_ctx} - default_params = key_in_params ? default_ctx[:params][key_in_params] : default_ctx[:params] - # Remove {:exclude} variables from the {params:} part - filtered_default_params = - if exclude - default_params.slice(*(default_params.keys - exclude)) - else - default_params # use original params if no filtering configured. - end - - # FIXME: very, very redundant. - default_params_for_ctx = key_in_params ? {key_in_params => filtered_default_params} : filtered_default_params - - ctx = default_ctx.merge({params: default_params_for_ctx}) - else # FIXME: if/else here sucks. - ctx = {} - end - - ctx = Assert.merge_for(ctx, merge_with_ctx, true) # merge injections - - Trailblazer::Test::Context[ctx] # this signals "pass-through" - end end # Assert # @private diff --git a/test/assertion_test.rb b/test/assertion_test.rb index 750fb15..8ade12b 100644 --- a/test/assertion_test.rb +++ b/test/assertion_test.rb @@ -145,7 +145,9 @@ def validate(ctx, params:, **) include Trailblazer::Test::Assertion it "#assert_fail" do - assert_fail Update, {params: {bla: 1}}, [:title] + # assert_fail Update, {params: {bla: 1}}, [:title] + # assert_fail Update, {params: {bla: 1}} do |result| + # end test = Class.new(Test) do @@ -166,7 +168,6 @@ def validate(ctx, params:, **) {title: ["is missing"]} end - # test_0003_anonymous it do assert_fail Update, {params: {record: true}}, [:title] @@ -178,6 +179,21 @@ def validate(ctx, params:, **) # expected: {title: ["is XXX"]} # this is wrong. end + + # test_0005_anonymous + it do + assert_fail Update, {params: {title: nil}} do |result| + @_m = true + assert_equal result[:"contract.default"].errors.messages, {:title=>["is missing"]} + end + end + + # test_0006_anonymous + it do + assert_fail Update, {params: {record: true}} do |result| # this actually passes. + @_m = true + end + end end test_1 = test.new(:test_0001_anonymous) @@ -195,11 +211,24 @@ def validate(ctx, params:, **) Expected: false Actual: true>) - test_4 = test.new(:test_0004_anonymous) + test_4 = test.new(:test_0004_anonymous) failures = test_4.() assert_equal failures.size, 1 failures[0].inspect.must_equal %(#[\"is missing\"]}\e[0m. Expected: {:title=>[\"is XXX\"]} Actual: {:title=>[\"is missing\"]}>) + + test_5 = test.new(:test_0005_anonymous) + failures = test_5.() + assert_equal test_5.instance_variable_get(:@_m), true + assert_equal failures.size, 0 + + test_6 = test.new(:test_0006_anonymous) + 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 %(#) end end