I have followed the StoreKit test setup guide and it is worked in integrated test.
After that, I want to create unit test about SKPaymentTransactionObserver.
I have write the unit test code like below.
import XCTest
import StoreKitTest
@available(iOS 14.0, *)
final class SimpleSKProductTransactionObserverTest: XCTestCase {
var sut: MockSKProductTransactionObserver!
var session: SKTestSession!
override func setUpWithError() throws {
session = try SKTestSession(configurationFileNamed: "ShopStoreKitConfig")
session.resetToDefaultState()
session.disableDialogs = true
session.clearTransactions()
sut = .init()
SKPaymentQueue.default().add(sut)
}
override func tearDownWithError() throws {
SKPaymentQueue.default().remove(sut)
sut = nil
session = nil
}
func testPaymentQueue() throws {
let productId = "prod_sale1_battery_1_day"
try session.buyProduct(productIdentifier: productId)
XCTAssertTrue(sut.isPaymentQueueCalled)
XCTAssertEqual(sut.previousTransactions.first?.payment.productIdentifier, productId)
}
}
class MockSKProductTransactionObserver: NSObject, SKPaymentTransactionObserver {
var previousTransactions = [SKPaymentTransaction]()
var isPaymentQueueCalled = false
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
isPaymentQueueCalled = true
previousTransactions = transactions
}
}
However, it is failed the assert codes.
/Users/jooyoulkim/git/rp_ios/PickaTests/SimpleSKProductTransactionObserverTest.swift:39 testPaymentQueue(): XCTAssertTrue failed
/Users/jooyoulkim/git/rp_ios/PickaTests/SimpleSKProductTransactionObserverTest.swift:40 testPaymentQueue(): XCTAssertEqual failed: ("nil") is not equal to ("Optional("prod_sale1_battery_1_day")")
The other side, I can see that SKPaymentTransactionObserver added in AppDelegate is worked normally.
So, the question is why observer is not added. Please tell me if have any idea about it. Thanks.
3
0
1.4k