Skip to content
Katabench
Try free

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:

C#
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:

  1. An amount of zero or less throws ArgumentOutOfRangeException before any collaborator is called.
  2. If the order store already has the order, the result is Duplicate and the customer is not charged again. Retried requests are common, and a second charge is the worst possible bug here.
  3. Otherwise the gateway is charged the exact amount, in cents. A declined payment returns Declined: nothing is saved and no receipt is sent.
  4. 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.
  5. A receipt is best effort. If Send throws, the order stays placed and PlaceOrder still returns Placed.

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

C#
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.

C# unit testing exercises →

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