Fork me on GitHub

Testability

To test code that uses a state machine, we first replace active state machines with passive ones. This can easily be achieved by injecting a IStateMachine into the class under test. In production code an active state machine is injected, in test code, a passive state machine is used. Passive and active state machine have exactly the same behavior but for one feature. Active state machines have their own worker thread. But tests are much simpler if they are pure synchronous, therefore this is a good thing for us during testing.

In production code, you should add handlers for the exception events of the state machine. In tests, you can use the fact that the state machine will re-throw any exception if there is no event handler registered. This makes sure that no exceptions are swallowed in your tests.