Skip to content

Testing Transitions with Minitest

CodeMeister edited this page Aug 11, 2026 · 2 revisions

testing transitions:

require the helper

include StateGate::Assertions

test the transitions

The matchers assert_allows_transitions and refute_allows_transitions test the transitions for a given attribute state.

  • assert_allows_transitions <class>, <state_gate_attribute>, from: <state>, to: [<state 1>, <state 2>, ...]
  • refute_allows_transitions <class>, <state_gate_attribute>, from: <state>, to: [<state 1>, <state 2>, ...]

With the follow configuration...

class Post < ActiveRecord::Base
  include StateGate

  state_gate :status do
    state :draft,     transitions_to: :pending
    state :pending,   transitions_to: [:draft, :published]
    state :published, transitions-to: :archived
    state :archived

    make_sequential
  end
end

...it passes with the correct transitions for the state.

assert_allows_transitions Post, :status, from: :draft, to: :pending
#=> success

assert_allows_transitions Post, :status, from: :pending, to: [:draft, :published]
#=> success

...it fails with a missing transition.

assert_allows_transitions Post, :status, from: :pending, to: :published
#=> fails: 'status also transitions from :pending to :draft.'

...it fails with a non-defined transition.

assert_allows_transitions Post, :status, from: :draft, to: :dummy
#=> fails: '#status does not transition from :draft to :dummy.'

...it fails with the wrong attribute.

assert_allows_transitions Post, :dummy, from: :pending, to: :draft
#=> fails: 'no state_gate is defined for #dummy.'

...refuting it passes with an invalid state.

refute_allows_transitions Post, :status, from: :draft, to: :archived
#=> success

Clone this wiki locally