Skip to content

Testing transitions with RSpec

CodeMeister edited this page Mar 7, 2023 · 3 revisions

testing transitions:

require the helper

Include the RSpec test helpers:

require 'state_gate/rspec'

test the transitions

The RSpec matcher allow_transitions_on tests the transitions for a given attribute state.

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.

expect(Post).to allow_transitions_on(:status).from(:draft).to(:pending)
#=> success

expect(Post).to allow_transitions_on(:status).from(:pending).to(:draft, :published)
#=> success

...it fails with a missing transition.

expect(Post).to allow_transitions_on(:status).from(:pending).to(:published)
#=> fails: ':pending is allowed to transition from :pending to :draft.'

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

expect(Post).to allow_transitions_on(:status).from(:draft).to(:pending, :dummy)
#=> fails: '#status does not transition from :draft to :dummy.'

...it fails with the wrong attribute.

expect(Post).to allow_transitions_on(:dummy).from(:pending).to(:draft, :Published)
#=> fails: 'no state_gate is defined for #dummy.'