Thursday, April 13, 2023

Casting in XCTest Methods

Paul Samuels:

The XCTest framework added the super helpful function XCTUnwrap back in Xcode 11. At the time I added a similar helper to my various projects to help smooth over the many cases where casting is required. The idea is to have a similar call site to XCTUnwrap but for use in situations where you want to verify you have the right type or fail.

His example also demonstrates using the new record(_ issue: XCTIssue) method, which lets you provide more information than just failing with a string description.

I added casting to my tests around the time I added unwrapping and find it very helpful. And you can often use type inference to avoid having to pass in the type.


Comments RSS · Twitter · Mastodon

Leave a Comment