---
title: "14.6 Testing it"
url: https://una-lang.org/en/learn/testing/testing-it/
release: v0.4.0
edition: "0.2"
---

# 14.6 Testing it

## 14.6.1 One example per path

Each example covers one path through `checkout`, and each states both the outcome and the effects that matter for that path:

```una include="day-to-day/src/checkout.una#L129-L174" title="day-to-day/src/checkout.una" caption="the examples"
example "a cart under 50.00 pays shipping"
  do total([Line(sku: "MUG-03", quantity: 1, unit_cents: 4999)])
  expect result == 5489

example "a cart of two lamps is charged and mailed"
  given @inventory = shelf(empty_map().with(key: "LAMP-01", value: 4))
  given @payments = card(limit: 10000)
  given @mailer = outbox()
  do checkout([Line(sku: "LAMP-01", quantity: 2, unit_cents: 1990)], email: "ana@example.invalid")
  expect result == "PAY-4470"
  expect performed @mailer.send

example "an empty cart asks nobody anything"
  given @inventory = shelf(empty_map())
  given @payments = processor_down()
  given @mailer = outbox()
  do checkout([], email: "ana@example.invalid")
  expect fails EmptyCart
  expect not performed @inventory.reserve
  expect not performed @payments.charge
  expect not performed @mailer.send

example "a cart the warehouse cannot hold is never charged"
  given @inventory = shelf(empty_map().with(key: "DESK-02", value: 1))
  given @payments = card(limit: 100000)
  given @mailer = outbox()
  do checkout([Line(sku: "DESK-02", quantity: 2, unit_cents: 24900)], email: "ana@example.invalid")
  expect fails OutOfStock
  expect not performed @payments.charge

example "a declined card releases the stock and mails nothing"
  given @inventory = shelf(empty_map().with(key: "DESK-02", value: 1))
  given @payments = card(limit: 10000)
  given @mailer = outbox()
  do checkout([Line(sku: "DESK-02", quantity: 1, unit_cents: 24900)], email: "ana@example.invalid")
  expect fails Declined
  expect performed @inventory.release
  expect not performed @mailer.send

example "a card processor that does not answer is PaymentDown"
  given @inventory = shelf(empty_map().with(key: "LAMP-01", value: 4))
  given @payments = processor_down()
  given @mailer = outbox()
  do checkout([Line(sku: "LAMP-01", quantity: 2, unit_cents: 1990)], email: "ana@example.invalid")
  expect fails PaymentDown
  expect performed @inventory.release
```

Read them as a specification of the checkout.

## 14.6.2 The total and the happy path

Two examples test paths on which nothing fails:

- **The total.** A cart under 50.00 pays 4.90 of shipping: 49.99 plus 4.90 is 54.89.
- **The happy path.** Two lamps cost 44.70 with shipping, the processor's reference comes back, and a receipt is sent.

## 14.6.3 The failure paths: `expect fails`

Four examples each test a failure, one for each variant of `CheckoutError`:

- **An empty cart** fails with `EmptyCart`, and none of the three services is asked anything.
- **A cart the warehouse cannot hold** fails with `OutOfStock`, and the card is never charged.
- **A declined card** fails with `Declined`, the stock is released, and no receipt goes out.
- **A processor that does not answer** fails with `PaymentDown`, and the stock is released too.

## 14.6.4 What must not happen: `expect not performed`

The `expect not performed` lines carry as much weight as the `expect fails` lines. A checkout that charged the card before it reserved the stock would still fail with `OutOfStock` for the example of a cart the warehouse cannot hold, but `expect not performed @payments.charge` would catch the charge.

## 14.6.5 Two habits that keep examples useful

Two habits keep a set of examples like this one useful. First, each example states the effects that matter for its path and no more: the happy path checks that a receipt was sent, and leaves the stock to the paths where it can go wrong. Second, when a bug turns up, the first change is an example that fails because of it. [Exercise 3](https://una-lang.org/en/learn/testing/exercises/#exercise-3-put-back-what-was-reserved) does exactly that for the gap in the reservations, so the fix comes with the evidence that it works.

## 14.6.6 Running the examples

`una test .` prints:

```text include="day-to-day/expected/commands/test.txt"
$ una test .
example checkout "a cart under 50.00 pays shipping" ... ok
example checkout "a cart of two lamps is charged and mailed" ... ok
example checkout "an empty cart asks nobody anything" ... ok
example checkout "a cart the warehouse cannot hold is never charged" ... ok
example checkout "a declined card releases the stock and mails nothing" ... ok
example checkout "a card processor that does not answer is PaymentDown" ... ok

6 examples: 6 passed, 0 failed
```
