Skip to content

Commit

Permalink
add spec: false option so Yogi can use Minitest::Test, for
Browse files Browse the repository at this point in the history
whatever reasons :grimace:.
  • Loading branch information
apotonick committed Jan 25, 2025
1 parent dc74341 commit 8795b1d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
21 changes: 10 additions & 11 deletions lib/trailblazer/test/assertion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 26 additions & 6 deletions lib/trailblazer/test/suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions test/docs/suite_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions test/suite_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 8795b1d

Please sign in to comment.