Skip to content

Testing States with Minitest

CodeMeister edited this page Aug 11, 2026 · 1 revision

testing states:

require the helper

Include the Minitest test helpers:

include StateGate::Assertions

assert valid states

The assertions assert_has_states and refute_has_states both test the defined states for a given attribute.

  • assert_has_states <class>, <state_gate_attribute>, <state 1>, <state 2>, <state 3>, ...
  • refute_has_states <class>, <state_gate_attribute>, <state 1>, <state 2>, <state 3>, ...

With the follow configuration...

class Post < ActiveRecord::Base
  include StateGate

  state_gate :status do
    state :draft
    state :pending
  end
end

...it passes with the correct states and attribute.

assert_has_states(Post, :status, :draft, :pending)
#=> success

...it fails with a missing state.

assert_has_states(Post, :status, :pending)
#=> fails: ':draft is also a valid state for #status.'

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

assert_has_states(Post, :status, :draft, :dummy)
#=> fails: ':dummy is not a valid state for #status.'

...it fails with the wrong attribute.

assert_has_states(Post, :category, :draft, :pending)
#=> fails: 'no state_gate is defined for #category.'

...refuting it passes with an invalid state.

refute_has_states(Post, :status, :dummy)
#=> success

Clone this wiki locally