pluginaweek / state_machine

Adds support for creating state machines for attributes on any Ruby class
http://www.pluginaweek.org
MIT License
3.74k stars 507 forks source link

DSL to test state_machine #299

Closed ka8725 closed 10 years ago

ka8725 commented 10 years ago

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

the8472 commented 10 years ago

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".

ka8725 commented 10 years ago

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