Hi,
We are using FSM actors and I was kind of missing handy features in Akka to more easily test these state machines.
What I wanted was a way to verify events are properly handled when FSM is in a certain state, I also want to verify the behaviour of the FSM is all other possible states.
So, to support this more event driven testing, I created a simple DSL which generates ScalaTest cases to verify the handing of an event in all possible states.
In it's simplest version it looks like this:
handleEvent(AnEvent()).inStates(State1, State2).verify { fsm =>
fsm.stateName should be(State3)
}.otherwiseKeepCurrentState
This tests whether the handling of AnEvent in both State1 and State2 results in a state transition to State3. And in all other states it is expected that the events does not cause a state change.
It is also possible to setup states with a specific data value, or receive not only the fsm actor, but also the event and the start state in the verify function, or do a more comprehensive verification for the other states:
handleEvent(AnotherEvent(30)).inStatesWithData(Map(
State1 -> defaultData,
State2 -> deviceData.copy(progress = Option(20))
)).verifyAll { case (fsm, event, startState) =>
fsm.stateName should be (DownloadInProgress)
fsm.stateData.progress.value should be (30)
}.otherwise { case (fsm, event, startState) =>
// do a more comprehensive verification. E.g. based on the startState.
}
Using this simple DSL, I was able to very quickly define test cases for all events supported by the state machines.
This was very helpful in to then, as a proper TDD developer ;-), implement the FSM actors.
Another 'problem' I encountered while writing test cases for FSM, was that in certain end-states, I want to verify the FSM chanced into the end-state and that actor is also stopped.
With the current TestFSMRef this is not possible, since the 'stateName' and 'stateData' properties are not available anymore after the has stopped.
To overcome this issue, I created a extension of the TestFSMRef which monitors the state transitions of the FSM actor under test. This allows us not only to verify the latest state of the FSM and whether the actor was properly stopped, but this also allows more easy testing of complex FSMs with automatic state transitions since this extension gives access to the full transition log, including the data at each state.
I am more than happy to share these features?
Would this DSL and TestFSMRef extension be a nice addition to the Akka framework?
Any chance this would be accepted it I would make this into a contribution to the Akka framework? Just would like to know whether spending time on that would not be wasted time.
Regards,
Joost