swiftunit-testingstorekitmatlab-spmstorekittest

I can't find the Storekit configuration file for unit test


I'm writing a library for Storekit. I want to write a unittest of my code. But I cannot see the storekit configuration file I added. And I get the following error: testFetchProducts(): failed: caught error: "Error Domain=SKTestErrorDomain Code=4 "File not found" UserInfo={NSLocalizedDescription=File not found}". How can I find the storekit configuration file?

Note, this is not an application, it is a library written with swift package.

Manifest file

.testTarget(
            name: "MyStoreTests",
            dependencies: ["MyStore"],
            resources: [
                .copy("Resources/JSON/test_list_response.json"),
                .copy("Resources/JSON/test_details_response.json"),
                .copy("Resources/JSON/login_response.json"),
                .copy("Resources/StoreConfiguration.storekit")
            ]
        )

Unit Test

import XCTest
import StoreKitTest

@available(iOS 14.0, *)
final class StoreKit1Tests: XCTestCase {

    private var testSession: SKTestSession!
    
    override func setUp() async throws {
        if self.testSession == nil {
            try await self.configureTestSession()
        }
        try await super.setUp()
    }

    override func tearDown() async throws {
        if let testSession = self.testSession {
            testSession.clearTransactions()
        }

        try await super.tearDown()
    }
    
    
    func configureTestSession() async throws {
        assert(self.testSession == nil, "Attempted to configure session multiple times")

        self.testSession = try SKTestSession(configurationFileNamed: "StoreConfiguration")
        self.testSession.resetToDefaultState()
        self.testSession.disableDialogs = true
        self.testSession.clearTransactions()
    }
    
    func testFetchProducts() {
        
    }

}

This is configuration file root

enter image description here

I created StoreKit Configuration file then I added products. I tried write unit test.


Solution

  • I had the same problem and to resolve the issue of the missing StoreKit configuration file in your test target, you need to configure SKTestSession with the URL of the configuration file rather than just the file name:

    @available(iOS 15.4, *)
    final class PurchasesTests: XCTestCase {
        let products = Set(["com.app.p1", "com.app.p2", "com.app.p3"])
        
        func testProductFetching() async throws {
            let url = Bundle.module.url(forResource: "StoreConfiguration", withExtension: "storekit")!
            let session = try SKTestSession(contentsOf: url)
            let products = try await Product.products(for: products)
            
            XCTAssertTrue(products.count == 3)
        }
    }