Test the Checkout Service
Medium About 35 min Free, no account needed
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.
The brief
A checkout service rarely computes anything interesting itself. Its job is to call its collaborators in the right order with the right arguments, and to stop calling them when something goes wrong. Return values cannot show that. Your job is to write the tests that prove this service behaves, using small fakes you write yourself in place of the payment gateway, the order store, and the receipt sender.
Here is the public API:
public enum CheckoutResult { Placed, Declined, Duplicate }
public interface IPaymentGateway
{
bool Charge(string orderId, long amountCents); // true when the payment is approved
}
public interface IOrderStore
{
bool Exists(string orderId);
void Save(string orderId, long amountCents);
}
public interface IReceiptSender
{
void Send(string orderId); // may throw when the mail provider is down
}
public class CheckoutService
{
public CheckoutService(IPaymentGateway payments, IOrderStore orders, IReceiptSender receipts) { ... }
public CheckoutResult PlaceOrder(string orderId, long amountCents) { ... }
}PlaceOrder follows these rules:
- An amount of zero or less throws
ArgumentOutOfRangeExceptionbefore any collaborator is called. - If the order store already has the order, the result is
Duplicateand the customer is not charged again. Retried requests are common, and a second charge is the worst possible bug here. - Otherwise the gateway is charged the exact amount, in cents. A declined payment returns
Declined: nothing is saved and no receipt is sent. - An approved payment is saved first and receipted second, so a receipt never points at an order
that was not stored. The result is
Placed. - A receipt is best effort. If
Sendthrows, the order stays placed andPlaceOrderstill returnsPlaced.
Saving first does not promise that the receipt arrives: if the process dies between the save and the send, the customer never hears back. This service accepts that gap because receipts are best effort. Commit State and Outbox Together shows how an outbox closes it when a message must not be lost.
Write a Tests class. Every public, parameterless method is a test; a test passes if it does not
throw. Your fakes are ordinary classes that implement the interfaces, and they can sit next to
Tests in the same file. A fake that records each call it receives turns "what did the service
do?" into something you can assert. Use Assert.Equal, True, False, and Throws<TException>
as needed (no using needed for Assert). Your suite must pass on the correct CheckoutService
and catch every hidden mutant, a planted-bug version you never see.
Starter code
public class Tests
{
private sealed class ApproveEverything : IPaymentGateway
{
public bool Charge(string orderId, long amountCents) => true;
}
private sealed class EmptyStore : IOrderStore
{
public bool Exists(string orderId) => false;
public void Save(string orderId, long amountCents) { }
}
private sealed class SilentReceipts : IReceiptSender
{
public void Send(string orderId) { }
}
public void An_approved_order_is_placed()
{
var checkout = new CheckoutService(new ApproveEverything(), new EmptyStore(), new SilentReceipts());
Assert.Equal(CheckoutResult.Placed, checkout.PlaceOrder("A-100", 2500));
}
}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.
Related guides
What it practices
- Test doubles
- Use fakes, stubs and recording collaborators to isolate and observe a unit.
- Error path testing
- Prove exceptions, retries and rejected inputs behave as specified.
Related katas
- Test the Retry Executor Hard Free You're given a correct retry executor. Write tests that prove its attempt budget, exception policy, and call sequence.
- Test the Order Status Workflow Medium Free You're given a correct order workflow. Write the tests that cover every state and call, and prove each refused move changes nothing.
- 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.