diff --git a/lib/trailblazer/test/assertion.rb b/lib/trailblazer/test/assertion.rb index 5740e49..782ae57 100644 --- a/lib/trailblazer/test/assertion.rb +++ b/lib/trailblazer/test/assertion.rb @@ -3,17 +3,16 @@ module Test # Top-level entry points for end users. # These methods expose the end user syntax, not the logic. module Assertion - def self.module!(receiver, activity: false, suite: false) - modules = - if activity && suite - [Helper::MockStep, AssertExposes, Suite, Assertion::Activity] - elsif activity - [Helper::MockStep, AssertExposes, Assertion, Assertion::Activity] - elsif suite # operation - [Helper::MockStep, AssertExposes, Suite] - else - [Helper::MockStep, AssertExposes, Assertion] - end + def self.module!(receiver, activity: false, suite: false, spec: true) + modules = [Helper::MockStep, AssertExposes] + if suite + modules += [Suite, Suite::Spec] if spec + modules += [Suite, Suite::Test] if suite && !spec + else + modules += [Assertion] + end + + modules += [Assertion::Activity] if activity receiver.include(*modules.reverse) end diff --git a/lib/trailblazer/test/suite.rb b/lib/trailblazer/test/suite.rb index 6ef2036..a6ecb73 100644 --- a/lib/trailblazer/test/suite.rb +++ b/lib/trailblazer/test/suite.rb @@ -5,13 +5,33 @@ module Test # in this module. module Suite # Defaults so tests run without tweaking (almost). - def self.included(includer) - includer.let(:operation) { raise "Trailblazer::Test: `let(:operation) { ... }` is missing" } - includer.let(:key_in_params) { false } - includer.let(:expected_attributes) { {} } # You need to override this in your tests. - includer.let(:default_ctx) { {} } + module Spec + def self.included(includer) + includer.let(:operation) { raise "Trailblazer::Test: `let(:operation) { ... }` is missing" } + includer.let(:key_in_params) { false } + includer.let(:expected_attributes) { {} } # You need to override this in your tests. + includer.let(:default_ctx) { {} } + end + end + + # For Minitest::Test, given there are still people using this awkward syntax. :) + module Test + def operation + raise "Trailblazer::Test: `def operation` is missing" + end + + def key_in_params + false + end + + def expected_attributes + {} + end + + def default_ctx + {} + end end - # TODO: only do this for Spec. # The assertions and helpers included into the actual test. def assert_pass(params_fragment, expected_attributes_to_merge={}, assertion: Assertion::AssertPass, **kws, &block) diff --git a/test/docs/suite_test.rb b/test/docs/suite_test.rb index ae96d43..9f0e549 100644 --- a/test/docs/suite_test.rb +++ b/test/docs/suite_test.rb @@ -271,3 +271,21 @@ class Create < Trailblazer::Operation # #:policy_fail-custom-name end # end # end + +# Test that the Suite module also works with a Minitest::Test class. +#:test-suite +class MemoCreateTest < Minitest::Test + Trailblazer::Test::Assertion.module!(self, suite: true, spec: false) + #~skip + Memo = Trailblazer::Test::Testing::Memo + #~skip end + def operation; Memo::Operation::Create end + def default_ctx; {params: {memo: {title: "Note to self", content: "Remember me!"}}} end + def expected_attributes; {title: "Note to self", content: "Remember me!"} end + def key_in_params; :memo end + + def test_our_assertions + assert_pass({}, {}) + end +end +#:test-suite end diff --git a/test/suite_test.rb b/test/suite_test.rb index 8636f24..2745938 100644 --- a/test/suite_test.rb +++ b/test/suite_test.rb @@ -417,6 +417,24 @@ def model(ctx, params:, **) end end +# Test that the Suite module also works with a Minitest::Test class. +# DISCUSS: should we use the above test tactics and test this inside a test with {#assert_test_case_passes} etc? +class SuiteInNonSpecTest < Minitest::Test + Trailblazer::Test::Assertion.module!(self, suite: true, spec: false) + + Memo = Trailblazer::Test::Testing::Memo + + def operation; Memo::Operation::Create end + def default_ctx; {params: {memo: {title: "Note to self", content: "Remember me!"}}} end + def expected_attributes; {title: "Note to self", content: "Remember me!"} end + def key_in_params; :memo end + + + def test_our_assertions + assert_pass({}, {}) + end +end + # class AssertionActivityTest < Minitest::Spec # Trailblazer::Test::Assertion.module!(self, activity: true)