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

# 12.6 Testing it

## 12.6.1 Stand-in services for every path

An example cannot write `with`, since its `do` line is a single call. The direct way for it to install each handler its `do` line needs is a `given` line, such as `given @mailer = outbox()`, which [section 14.3](https://una-lang.org/en/learn/testing/fakes-given-expect-performed-and-expect-not-performed/#s14-3) covers in full. With the handlers of this chapter, every path of `notify` can be tested on demand: a customer who wants notices, an unknown customer, a customer who opted out, a directory that does not answer, and a mail service that does not answer.

```una include="day-to-day/src/notices.una#L110-L138" title="day-to-day/src/notices.una" caption="the examples of notify"
example "a customer who wants notices gets one"
  given @customers = directory(sample_customers())
  given @mailer = outbox()
  do notify(Shipment(order: "A-1001", customer: "C-17", carrier: "Parcelo"))
  expect result == "rosa@example.invalid"

example "an unknown customer is rejected before any mail is sent"
  given @customers = directory(sample_customers())
  given @mailer = mail_down()
  do notify(Shipment(order: "A-1003", customer: "C-99", carrier: "Parcelo"))
  expect fails UnknownCustomer

example "a customer who asked for no notices is rejected before any mail is sent"
  given @customers = directory(sample_customers())
  given @mailer = mail_down()
  do notify(Shipment(order: "A-1004", customer: "C-21", carrier: "Parcelo"))
  expect fails OptedOut

example "a directory that does not answer is DirectoryDown, before any mail is sent"
  given @customers = directory_down()
  given @mailer = mail_down()
  do notify(Shipment(order: "A-1001", customer: "C-17", carrier: "Parcelo"))
  expect fails DirectoryDown

example "a mail service that does not answer is MailDown"
  given @customers = directory(sample_customers())
  given @mailer = mail_down()
  do notify(Shipment(order: "A-1001", customer: "C-17", carrier: "Parcelo"))
  expect fails MailDown
```

The happy path expects the address with `expect result`, and each failure path names its variant with `expect fails`, as in [section 7.8](https://una-lang.org/en/learn/failure/testing-it/#the-failure-paths-expect-fails).

## 12.6.2 A failure that comes first

Three examples install `mail_down()` on purpose. Had `notify` sent any mail before it failed, the mail service would have failed first, and the example would see `MailDown` instead of the variant it expects. So each of them also shows that the directory's answer is checked before any mail goes out.

## 12.6.3 The pure parts, with values alone

`notice` and `log_line` hold no binding, so their examples need no handler at all: values in, a text out.

```una include="day-to-day/src/notices.una#L140-L146" title="day-to-day/src/notices.una" caption="the examples of the pure functions"
example "the notice names the customer, the order and the carrier"
  do notice(Customer(id: "C-17", name: "Rosa", email: "rosa@example.invalid", wants_notices: true), shipment: Shipment(order: "A-1001", customer: "C-17", carrier: "Parcelo"))
  expect result == "Hello Rosa, your order A-1001 is on its way with Parcelo."

example "a skipped shipment says why in the log"
  do log_line(Shipment(order: "A-1005", customer: "C-17", carrier: "Rapida"), outcome: Err(MailDown))
  expect result == "skipped  A-1005: the mail service did not answer; retry later"
```

`dispatch` prints through `@log`, and it is not tested here: an example cannot use the host's console, and checking which operations a call performed is a tool of [chapter 14](https://una-lang.org/en/learn/testing/).

## 12.6.4 Running the examples

`una test .` prints:

```text include="day-to-day/expected/commands/test.txt#L2-L10"
example notices "a customer who wants notices gets one" ... ok
example notices "an unknown customer is rejected before any mail is sent" ... ok
example notices "a customer who asked for no notices is rejected before any mail is sent" ... ok
example notices "a directory that does not answer is DirectoryDown, before any mail is sent" ... ok
example notices "a mail service that does not answer is MailDown" ... ok
example notices "the notice names the customer, the order and the carrier" ... ok
example notices "a skipped shipment says why in the log" ... ok

7 examples: 7 passed, 0 failed
```
