Test the Order Status Workflow
Medium About 30 min Free, no account needed
You're given a correct order workflow. Write the tests that cover every state and call, and prove each refused move changes nothing.
The brief
An order moves through a small state machine: it is paid, shipped, and delivered, and it can be cancelled before it ships. Every other move must be refused. The workflow here is already written and correct. Your job is to write the tests that prove it, including the moves it must refuse.
Here is the public API:
public enum OrderStatus
{
Pending,
Paid,
Shipped,
Delivered,
Cancelled
}
public class OrderWorkflow
{
public OrderStatus Status { get; } // starts at Pending
public bool RefundDue { get; } // starts false
public void Pay() { ... }
public void Ship() { ... }
public void Deliver() { ... }
public void Cancel() { ... }
}The legal transitions are the whole contract:
| Call | Allowed from | Moves to |
|---|---|---|
Pay() |
Pending |
Paid |
Ship() |
Paid |
Shipped |
Deliver() |
Shipped |
Delivered |
Cancel() |
Pending or Paid |
Cancelled |
Cancelling a Paid order sets RefundDue to true, because the customer's money has to go back.
Cancelling a Pending order owes nothing. Delivered and Cancelled are final.
Any call from a status the table does not allow throws InvalidOperationException, and a refused
call changes nothing: Status and RefundDue keep the values they had before it.
var order = new OrderWorkflow();
order.Pay();
Assert.Equal(OrderStatus.Paid, order.Status);Write a Tests class. Every public, parameterless method is a test; a test passes if it does not
throw. Use Assert.Equal, True, False, and Throws<TException> as needed (no using needed
for Assert, though InvalidOperationException lives in System). Your suite must pass on the
correct OrderWorkflow and catch every hidden mutant, a planted-bug version you never see.
This workflow guards its moves at runtime and throws. Two design puzzles take the same kind of lifecycle further: Adopt the State That Comes Back gives each status its own object, and Make Illegal States Unrepresentable removes the illegal moves from the types so there is nothing left to refuse. Whatever the design, a suite like this one pins down what the table allows.
Starter code
public class Tests
{
public void Paying_a_new_order_marks_it_paid()
{
var order = new OrderWorkflow();
order.Pay();
Assert.Equal(OrderStatus.Paid, order.Status);
}
}How it is graded
You write the tests. A suite passes only when it stays green on correct code and turns enough faulty implementations red.
What it practices
- Stateful testing
- Drive call sequences and state-machine transitions, not just single calls.
- Error path testing
- Prove exceptions, retries and rejected inputs behave as specified.
Related katas
- Test the Checkout Service Medium Free You're given a correct checkout service with three collaborators. Write recording fakes that prove what it does to each one, and in what order.
- Test Stable Priority Ordering Medium Free Write operation sequences that distinguish priority order, FIFO ties, peeking, and queue-instance isolation.
- Test Unicode Scalar Counting Medium Free Distinguish UTF-16 code units from Unicode scalars and reject unpaired surrogates at every position.
- Test the Circuit Breaker State Machine Hard Free Drive closed, open, and half-open transitions with a fake clock and prove rejected calls never execute.