pluginaweek/state_machine

DSL to test state_machine

Closed this issue · 2 comments

It would be great to have some DSL to test the defined state machine on the model. For example, in our project there is a custom matcher which allows to test the states like this:

it 'has #to_installing event' do
  should have_event(state_field, :to_installing, {
    [:ordered, :installation_failed] => :installing
  })
end

If you are interested in this solution I'm ready to contribute this feature. Please, let me know. Thanks

it should be trivial to write a matcher for that. reflection is possible on the entire state machine. you can access available transitions via class.state_machines[:machine_name].events[:event_name].branches

Do note that fields != machines. as multiple machines can access the same attribute.

And wouldn't it be better to actually test the transition performing correctly, including side-effects instead of just checking whether it's declared?
If you simply check the existence you're basically writing down the same definitions twice (once in the state_machine DSL, once in the tests). I.e. you only end up testing whether the state_machine DSL "works".

Thanks for the tip, you are right about checking real states changes