Skip to content

Add example for how to test any error is thrown in the error testing documentation article #853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ test that the code throws an error of a given type, or matches an arbitrary
Boolean test. Similar overloads of ``require(_:_:sourceLocation:)-5l63q`` stop
running your test if the code doesn't throw the expected error.

### Validate that your code throws any error

To check that the code under test throws an error of any type, pass
`(any Error).self` as the first argument to either
``expect(throws:_:sourceLocation:performing:)-1xr34`` or
``require(_:_:sourceLocation:)-5l63q``:

```swift
@Test func cannotAddToppingToPizzaBeforeStartOfList() {
var order = PizzaToppings(bases: [.calzone, .deepCrust])
#expect(throws: (any Error).self) {
try order.add(topping: .mozarella, toPizzasIn: -1..<0)
}
}
```

### Validate that your code doesn't throw an error

A test function that throws an error fails, which is usually sufficient for
Expand Down