Skip to content

Commit e15c39e

Browse files
committed
More docs
1 parent f139694 commit e15c39e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Sources/Testing/Testing.docc/testing-for-errors-in-swift-code.md

+19
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,22 @@ If the closure throws _any_ error, the testing library records an issue.
6666
If you need the test to stop when the code throws an error, include the
6767
code inline in the test function instead of wrapping it in a call to
6868
``expect(throws:_:sourceLocation:performing:)-7du1h``.
69+
70+
## Inspect an error thrown by your code
71+
72+
When you use `#expect(throws:)` or `#require(throws:)` and the error matches the
73+
expectation, it is returned to the caller so that you can perform additional
74+
validation. If the expectation fails because no error was thrown or an error of
75+
a different type was thrown, `#expect(throws:)` returns `nil`:
76+
77+
```swift
78+
@Test func cannotAddMarshmallowsToPizza() throws {
79+
let error = #expect(throws: PizzaToppings.InvalidToppingError.self) {
80+
try Pizza.current.add(topping: .marshmallows)
81+
}
82+
#expect(error?.topping == .marshmallows)
83+
#expect(error?.reason == .dessertToppingOnly)
84+
}
85+
```
86+
87+
If you aren't sure what type of error will be thrown, pass `(any Error).self`.

0 commit comments

Comments
 (0)