← All writing

SKTestSession testing failures

With iOS 17, Apple shipped a new API allows us to simulate errors when unit testing StoreKit. The API exposes a method called setSimulatedError(_ error: API.Failure?, forAPI api: API) which is intended to be used on a instance of a SKTestSession.

For example, we would use the API to simulate a NetworkError for the purchase API.

try await session.setSimulatedError(.generic(.networkError(URLError(.badURL, userInfo: [:]))), forAPI: .purchase)

This is a great addition and it allows us to test possible error handling flows we have in our code. One thing I encountered when working with this API is that the simulated errors would persist even if I called

 try await session.setSimulatedError(nil, forAPI: .purchase)

as outlined in the WWDC session.

The current workaround for this is to call the resetToDefaultState method on the session before each test executes.

session.resetToDefaultState()

Until next time, M.

Filed underXcode, Swift, iOS, StoreKit 2, setSimulatedError, SKTestSession