Filed at the same time of posting this. Sorry, didn’t paste the ID earlier. FB15309950
Post
Replies
Boosts
Views
Activity
Here is my code
import StoreKit
import StoreKitTest
import Testing
@testable import StoreKitSwiftTesting
final class StoreKitSwiftTestingTests {
// Intentionally setting to something different from StoreKit Config default "USA"
enum Country: String, CaseIterable {
case north = "CAN"
case south = "MEX"
}
let session: SKTestSession
init() {
session = try! SKTestSession(configurationFileNamed: "Store") // Set default to "USA" in Store.storekit configuration file
session.disableDialogs = true
}
deinit {
session.resetToDefaultState()
print("⛔️ session country: \(session.storefront) -- deinit")
}
@Test(.serialized, arguments: Country.allCases)
func countryCode(for country: Country) async throws {
print("✅ received country: \(country.rawValue)")
session.storefront = country.rawValue
print("\(session.storefront == country.rawValue ? "✅" : "❌") country on session: \(session.storefront)")
let countryCode = try #require(await Storefront.current?.countryCode)
print("\(countryCode == country.rawValue ? "✅" : "❌") Storefront Country: \(countryCode)")
#expect(countryCode == country.rawValue)
}
}
Here is the stdout
◇ Test run started.
↳ Testing Library Version: 94 (arm64-apple-ios13.0-simulator)
◇ Suite StoreKitSwiftTestingTests started.
◇ Test countryCode(for:) started.
◇ Passing 1 argument country → .north to countryCode(for:)
✅ received country: CAN
✅ country on session: CAN
✅ Storefront Country: CAN
⛔️ session country: USA -- deinit
◇ Passing 1 argument country → .south to countryCode(for:)
✅ received country: MEX
✅ country on session: MEX
❌ Storefront Country: USA
✘ Test countryCode(for:) recorded an issue with 1 argument country → .south at StoreKitSwiftTestingTests.swift:41:9: Expectation failed: (countryCode → "USA") == (country.rawValue → "MEX")
⛔️ session country: USA -- deinit
✘ Test countryCode(for:) failed after 0.027 seconds with 1 issue.
✘ Suite StoreKitSwiftTestingTests failed after 0.027 seconds with 1 issue.
✘ Test run with 1 test failed after 0.027 seconds with 1 issue.
As you can see the first run of the test function seems as expected, it prints "CAN" for all print statements which is correct. When the first run completes, the deinit gets called which resets the session. Resetting the session causes session to get values from the storekit config file which has "USA". But in the second run of the test function with param value of "MEX", it does not change the Storefront countryCode but instead keeps it to reset default "USA"
Ok great. Glad to hear it was an issue that is now fixed. Thank you.
If anybody stumbles upon this, this is what I ended up doing for my app. In CoreData model, I declared an attribute ColorData of type Binary Data and in the model extension, I wrote a property like this
#if os(iOS)
import UIKit
#endif
#if os(macOS)
import Foundation
#endif
extension Model {
	private struct ColorData: Codable {
		var r: Double
		var g: Double
		var b: Double
		var a: Double
	}
	var color: Color {
		get {
			guard let data = colorData, let decoded = try? JSONDecoder().decode(ColorData.self, from: data) else { return Color(assetName: .accent) }
			return Color(.sRGB, red: decoded.r, green: decoded.g, blue: decoded.b, opacity: decoded.a)
		}
		set(newColor) {
			#if os(iOS)
			let nativeColor = UIColor(newColor)
			#elseif os(macOS)
			let nativeColor = NSColor(newColor)
			#endif
			var (r, g, b, a) = (CGFloat.zero, CGFloat.zero, CGFloat.zero, CGFloat.zero)
			nativeColor.getRed(&r, green: &g, blue: &b, alpha: &a)
			if let encoded = try? JSONEncoder().encode(ColorData(r: Double(r), g: Double(g), b: Double(b), a: Double(a))) {
				colorData = encoded
			}
		}
	}
}
Having this ready, you can simply read and write SwiftUI Color to the model object
let obj = Model(context: context)
obj.color = .blue // This will call the setter which will take care of converting Color to Data to store in CoreData
This may not be the best solution for all cases but it works for me so I thought I would share.
Well, reinstalling Xcode solved my problem