Have you tried adding session.resetToDefaultState() to the setupError?
override func setUpWithError() throws {
session = try SKTestSession(configurationFileNamed: "Configuration")
session.resetToDefaultState()
session.clearTransactions()
session.disableDialogs = true
}
However, in some cases (like mine), you may need to add this line for each test block.
I don't know if this issue is caused by my logic or a bug in StoreKitTest.
override func setUpWithError() throws {
session = try SKTestSession(configurationFileNamed: "Configuration")
// Even if I add this in setUpWithError, a test will always return an unexpected error.
session.resetToDefaultState()
session.clearTransactions()
session.disableDialogs = true
}
func testExample() async throws {
// you'd need to insert this here but it depends on your code
session.resetToDefaultState()
session.clearTransactions()
session.disableDialogs = true
let products = try await Product.products(for: [productID])
let value = try await products[0].purchase()
switch value {
case .success(let verificationResult):
switch verificationResult {
case .verified(let signedType):
XCTAssertEqual(signedType.productID, productID)
case .unverified:
XCTFail()
}
case .userCancelled, .pending:
XCTFail()
@unknown default:
XCTFail()
}
}
The WWDC2023 session mentioned using setSimulatedError(nil) to disable the previous setting, but currently, only setting nil didn't work, as far as I know.
Refer to: WWDC2023 Session
Additionally, once you set an error with setSimulatedError, a unit test always returns the error that you set previously (Configuration, as SKSession is shared).