Home / Learn / 14 Testing
Release v0.4.0Edition 0.2
14.7 Common mistakes
Page 8 of 10
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)
Add an example that runs the whole program:
--- 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
[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 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)
Leave out the card processor in the happy-path example:
--- 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()
[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)
Write @mailer.sent for @mailer.send:
--- a/src/checkout.una
+++ b/src/checkout.una
@@ -138,3 +138,3 @@
expect result == "PAY-4470"
- expect performed @mailer.send
+ expect performed @mailer.sent
[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).
14.7.4 Expecting the service's failure (U1001)
Expect Refused, what the card processor says, instead of Declined, what the checkout says:
--- 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
[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)
Copy the first example and change only its body:
--- 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"
[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)
Move the card processor from a given line to the do line:
--- 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
[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.
Next:14.8 Exercises