Skip to content

Testing States with RSpec

CodeMeister edited this page Mar 7, 2023 · 2 revisions

testing states:

Rspec matcher have_states tests the defined states for a given attribute.

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.

expect(Post).to have_states(:draft, :pending).for(:status)
#=> success

...it fails with a missing state.

expect(Post).to have_states(:pending).for(:status)
#=> fails: ':draft is also a valid state for #status.'

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

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

...it fails with the wrong attribute.

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

Clone this wiki locally