How to test transition callbacks?

66 views
Skip to first unread message

apneadiving

unread,
Dec 31, 2011, 3:16:51 AM12/31/11
to pluginaw...@googlegroups.com

Hi,

I'd like to know what's the cleanest way to test that transition callbacks are being triggered.

For instance, how would you test:

    before_transition :parked => any - :parked, :do => :put_on_seatbelt

Thanks,

Ben

PS:

From now, I test the transitions this way:

doc.status = "waiting_assignment"                                                                                                    
doc.status_transitions.map(&:to).should eq ["in_progress", "paused"] 

Aaron Pfeifer

unread,
Dec 31, 2011, 12:52:54 PM12/31/11
to pluginaw...@googlegroups.com
Hey Ben -

I think that's a fairly good way to test the various possible transitions from a particular state.  I typically test transitions by setting the state for the object, and then validating that each event defined for the state machine does what should be done... e.g.

doc.status = "waiting_assigment"
doc.pause.should be_true
doc.should be_paused

doc.status = "in_progress"
doc.wait.should be_false
doc.should be_in_progress

# and so on and so forth...

There's nothing really built-in for testing, so to each their own :)

-Aaron

apneadiving

unread,
Jan 1, 2012, 4:24:22 AM1/1/12
to pluginaw...@googlegroups.com
Thanks Aaron and happy new year ;)

My PS about transitions was just for Information, my question concerns transition callbacks.

For instance from this code:

    before_transition :parked => any - :parked, :do => :put_on_seatbelt

how do you test that put_on_seatbelt is triggered during the proper transitions?

Ben

Aaron Pfeifer

unread,
Jan 2, 2012, 12:13:40 PM1/2/12
to pluginaw...@googlegroups.com
Hey Ben -

Similar to my previous comment, I don't inspect anything on the definition.  I just run the event and either validate that the method is called either by using a message expectation or just validating that result is as expected.  For example:

vehicle.should_receive(:put_on_seatbelt)
vehicle.ignite.should be_true

# ...alternatively...

vehicle.seatbelt_on = true
vehicle.ignite.should be_true
vehicle.seatbelt_on.should be_true

Again, there's nothing special that state_machine provides this.  It's just your typical testing method.

Best,
Aaron

apneadiving

unread,
Jan 2, 2012, 12:17:50 PM1/2/12
to pluginaw...@googlegroups.com
Ok, I feared what I did was not that clean because I thought you'd store the callbacks chain somewhere.

Thanks for your answer (again!).

Ram kumar

unread,
Dec 16, 2013, 8:08:17 AM12/16/13
to pluginaw...@googlegroups.com
This might work for begin_transition, but how to do it for after_transition?
Reply all
Reply to author
Forward
0 new messages