Ios 修改NSObjects的内部属性(特别是SKPaymentTransaction)
我正在尝试对我们的收据验证服务器进行单元测试,虽然我可以修改内部API以避免此问题,但这意味着我们没有完全测试客户端API,因此我希望避免这种情况 作为API的一部分,我们先传递一个SKPaymentTransaction,然后将Transaction.TransactionReceive传递到服务器 为了正确测试这一点,我想创建一个SKPaymentTransaction实例,并选择TransactionReceive(有效值和无效值) 不幸的是,SKPaymentTransaction将transactionReceipt属性定义为只读,并且我无法声明将其定义为readwrite的扩展/子类 我似乎也无法将SKPaymentTransaction指针强制转换为char*来手动将值注入内存,因为Xcode在ARC下不允许这样做 有没有人知道我如何才能实现我的目标 谢谢Ios 修改NSObjects的内部属性(特别是SKPaymentTransaction),ios,objective-c,xcode,unit-testing,skpaymenttransaction,Ios,Objective C,Xcode,Unit Testing,Skpaymenttransaction,我正在尝试对我们的收据验证服务器进行单元测试,虽然我可以修改内部API以避免此问题,但这意味着我们没有完全测试客户端API,因此我希望避免这种情况 作为API的一部分,我们先传递一个SKPaymentTransaction,然后将Transaction.TransactionReceive传递到服务器 为了正确测试这一点,我想创建一个SKPaymentTransaction实例,并选择TransactionReceive(有效值和无效值) 不幸的是,SKPaymentTransaction将tr
Lee发现我可以通过swizzle transactionReceipt getter将我自己的数据注入到调用中 所以我的结局是
-(void)test_function
{
SKPaymentTransaction* invalidTransaction = [[SKPaymentTransaction alloc] init];
Method swizzledMethod = class_getInstanceMethod([self class], @selector(replaced_getTransactionReceipt));
Method originalMethod = class_getInstanceMethod([invalidTransaction class], @selector(transactionReceipt));
method_exchangeImplementations(originalMethod, swizzledMethod);
// Call to receipt verification server
}
- (NSData*)replaced_getTransactionReceipt
{
return [@"blah" dataUsingEncoding:NSUTF8StringEncoding];
}
我写了一篇博客文章,展示了我的过程,并在这里提供了更多细节。I子类SKPaymentTransaction(例如MutableSKPaymentTransaction),覆盖只读参数。已经有一个可变的SKPaymentTransaction,您可以使用它,也可以用类似的方式覆盖SKPayment 例如: 在头文件(MutableSKPaymentTransaction.h)文件中
#import <StoreKit/StoreKit.h>
@interface MutableSKPaymentTransaction : SKPaymentTransaction
@property (readwrite, copy, nonatomic) NSError * error;
@property (readwrite, copy, nonatomic) SKPayment * payment;
@property (readwrite, copy, nonatomic) NSString * transactionIdentifier;
@property (readwrite, copy, nonatomic) NSDate * transactionDate;
@property (readwrite, copy, nonatomic) NSArray * downloads;
@property (readwrite, copy, nonatomic) SKPaymentTransaction *originalTransaction;
@property (assign, nonatomic) SKPaymentTransactionState transactionState;
@end
#import "MutableSKPaymentTransaction.h"
@implementation MutableSKPaymentTransaction
// readonly override
@synthesize error = _error;
@synthesize payment = _payment;
@synthesize transactionIdentifier = _transactionIdentifier;
@synthesize transactionDate = _transactionDate;
@synthesize downloads = _downloads;
@synthesize originalTransaction = _originalTransaction;
@synthesize transactionState = _transactionState;
@end