无法在 SKProductsResponse 模拟中设置无效的产品标识符



我正在尝试对我的支付模块进行单元测试,为此我试图模拟SKProductsResponse和它返回的属性。

我从产品属性开始,它工作正常。 但是当我尝试对运行时中断的无效产品标识符进行同样的事情时......这是代码

首先我尝试扩展类

class ProductsResponse:SKProductsResponse
{
init(products:[SKProduct], invalidProducts:[String])
{
super.init()
self.setValue(products, forKey: "products")
self.setValue(invalidProducts, forKey: "invalidProductIdentifiers")
}
}

然后通过制作扩展

extension SKProductsResponse
{
convenience init(products:[SKProduct], invalidProducts:[String]) {
self.init()
self.setValue(products, forKey: "products")
self.setValue(invalidProducts, forKey: "invalidProductIdentifiers")
}
}

两者都会导致相同的问题,但仅适用于 invalidProductIdentifiers ->捕获了"NSUnknownKeyException","[ProductsResponse setValue:forUndefinedKey:]:此类不符合键值编码的键无效产品标识符。

您知道为什么或这种方法的任何替代方案吗? 谢谢!

好吧,我仍然不知道发生了什么,但我意识到我可以简单地覆盖"无效产品标识符"的获取者,如下所示:

override var invalidProductIdentifiers: [String] {get{ return invalidIds}}
private var invalidIds:[String] = []
init(products:[SKProduct], invalidProducts:[String])
{
super.init()
invalidIds = invalidProducts
}

这很好用。

最新更新