---
title: "14.7 Common mistakes"
url: https://una-lang.org/en/learn/testing/common-mistakes/
release: v0.4.0
edition: "0.2"
---

# 14.7 Common mistakes

Each mistake below is a small change to `day-to-day/src/checkout.una`, followed by what `una check .` reports. `una test .` reports the same diagnostics and runs nothing, since a package that does not check has no examples to run.

## 14.7.1 Testing `main` (U3014)

:::mistake
Add an example that runs the whole program:

```diff include="day-to-day..mistakes/1:src/checkout.una" context="1" title="src/checkout.una"
--- a/src/checkout.una
+++ b/src/checkout.una
@@ -174 +174,5 @@
   expect performed @inventory.release
+
+example "the log shows four checkouts"
+  do main()
+  expect result == unit
```

```text include="mistakes/1/expected/rendered.txt#L1"
[U3014] Error: `main` needs `@console: Console[print]`, a host capability, and examples cannot use host capabilities; give it a handler with `given`
```

*Why:* `main` needs the console, a host capability, and an example never reaches the host. That is what keeps it deterministic. It happens when you want to test "the whole thing" at once. The message names the binding `main` needs, says why it is refused, and gives the repair.

*Fix:* give `@console` a handler of your own, as the help says, and as [A fake of a host capability](https://una-lang.org/en/learn/testing/fakes-given-expect-performed-and-expect-not-performed/#a-fake-of-a-host-capability) does: `handler quiet() for Console` with `op print(line) = unit`, then `given @console = quiet()` in the example. Better, test the functions `main` calls, `checkout` and `describe`, whose results an example can compare. A fake console holds no state, so it cannot show what was printed.
:::

## 14.7.2 A service with no `given` (U3001)

:::mistake
Leave out the card processor in the happy-path example:

```diff include="day-to-day..mistakes/2:src/checkout.una" context="1" title="src/checkout.una"
--- a/src/checkout.una
+++ b/src/checkout.una
@@ -134,3 +134,2 @@
   given @inventory = shelf(empty_map().with(key: "LAMP-01", value: 4))
-  given @payments = card(limit: 10000)
   given @mailer = outbox()
```

```text include="mistakes/2/expected/rendered.txt#L1"
[U3001] Error: `checkout` needs `@payments: Payments[charge]`, which no `given` line binds
```

*Why:* every capability the `do` line needs must be bound by a `given` line. An example never falls back to a real service. It happens when an example is copied from one that tested a function with fewer services. The message names the function and the binding, with its capability and operations, so you know which fake to install.

*Fix:* add `given @payments = card(limit: 10000)`.
:::

## 14.7.3 A misspelled operation (U1001)

:::mistake
Write `@mailer.sent` for `@mailer.send`:

```diff include="day-to-day..mistakes/3:src/checkout.una" context="1" title="src/checkout.una"
--- a/src/checkout.una
+++ b/src/checkout.una
@@ -138,3 +138,3 @@
   expect result == "PAY-4470"
-  expect performed @mailer.send
+  expect performed @mailer.sent
 
```

```text include="mistakes/3/expected/rendered.txt#L1,L8"
[U1001] Error: capability `Mailer` has no operation `sent`
     │ Help: repair 1: write `send`; edit src/checkout.una:139:28..139:32 to "send"
```

*Why:* `expect performed` names an operation of the capability, and the checker resolves it like a call. It happens because the line reads like English, where *sent* is natural. The help line gives the closest operation, and the exact edit.

*Fix:* `una fix .` applies the repair shown ([section 18.2](https://una-lang.org/en/learn/tools/reading-a-diagnostic/#s18-2)).
:::

## 14.7.4 Expecting the service's failure (U1001)

:::mistake
Expect `Refused`, what the card processor says, instead of `Declined`, what the checkout says:

```diff include="day-to-day..mistakes/4:src/checkout.una" context="1" title="src/checkout.una"
--- a/src/checkout.una
+++ b/src/checkout.una
@@ -163,3 +163,3 @@
   do checkout([Line(sku: "DESK-02", quantity: 1, unit_cents: 24900)], email: "ana@example.invalid")
-  expect fails Declined
+  expect fails Refused
   expect performed @inventory.release
```

```text include="mistakes/4/expected/rendered.txt#L1"
[U1001] Error: cannot find variant `Refused` in `CheckoutError`
```

*Why:* `expect fails` names a variant of the error kind of the `do` call, and `checkout` fails with `CheckoutError`. It translated `Refused` into `Declined`. It happens when you think of what went wrong, the refusal, rather than of what the caller sees. The message names the kind it searched, `CheckoutError`, which is the kind to read.

*Fix:* expect the caller's variant, `Declined`.
:::

## 14.7.5 Copying an example and keeping its name (U1002)

:::mistake
Copy the first example and change only its body:

```diff include="day-to-day..mistakes/5:src/checkout.una" context="1" title="src/checkout.una"
--- a/src/checkout.una
+++ b/src/checkout.una
@@ -132,2 +132,6 @@
 
+example "a cart under 50.00 pays shipping"
+  do total([Line(sku: "LAMP-01", quantity: 2, unit_cents: 1990)])
+  expect result == 4470
+
 example "a cart of two lamps is charged and mailed"
```

```text include="mistakes/5/expected/rendered.txt#L1"
[U1002] Error: example "a cart under 50.00 pays shipping" is defined twice in this module
```

*Why:* the name is how `una test` reports an example, so it is unique within its module. It happens whenever an example is copied to make a similar one. The message quotes the name, and the mark points at the second declaration.

*Fix:* name the case the new example covers, such as `"two lamps pay shipping"`.
:::

## 14.7.6 Installing a fake with `with` on a `do` line (U0101)

:::mistake
Move the card processor from a `given` line to the `do` line:

```diff include="day-to-day..mistakes/6:src/checkout.una" context="1" title="src/checkout.una"
--- a/src/checkout.una
+++ b/src/checkout.una
@@ -169,5 +169,4 @@
   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")
+  do checkout([Line(sku: "LAMP-01", quantity: 2, unit_cents: 1990)], email: "ana@example.invalid") with @payments = processor_down()
   expect fails PaymentDown
```

```text include="mistakes/6/expected/rendered.txt#L1"
[U0101] Error: unexpected keyword `with`, expected end of line
```

*Why:* a `do` line is one expression, and an example installs its handlers with `given` lines only. It happens because `with` is how `main` installs handlers. The message is a syntax error, U0101: Una read the call, and did not expect another word after it.

*Fix:* write `given @payments = processor_down()` above the `do` line.
:::
