AFNetworking 2.0:为 TDD 子类化 AFHTTPSessionManager 导致 AFURLRequestSerialization init 出错
AFNetworking 2.0: Subclassing AFHTTPSessionManager for TDD causing error in AFURLRequestSerialization init
我已经 subclassed AFHTTPSessionManager
以便在测试时使用它进行模拟。但是,我在 AFURLRequestSerialization
的 init
方法中一直 运行ning 进入 BAD_ACCESS 错误 - 仅使用模拟。这是设置:
测试class:
@interface PLBookCommunicatorTests : XCTestCase
@end
@implementation PLBookCommunicatorTests
{
MockAFHTTPSessionManager *httpSessionManager;
HKCommunicator *communicator;
}
- (void)setUp
{
[super setUp];
httpSessionManager = [[MockAFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://base.url"]];
communicator = [[HKCommunicator alloc] initWithHttpSessionManager:httpSessionManager];
}
MockAFHTTPSessionMannager.h
#import "AFHTTPSessionManager.h"
@interface MockAFHTTPSessionManager : AFHTTPSessionManager
@property (nonatomic, assign) BOOL successful;
@end
MockAFHTTPSessionManager.m
import <AFNetworking.h>
#import "MockAFHTTPSessionManager.h"
@implementation MockAFHTTPSessionManager
- (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
if(self.successful)
{
success(nil, nil);
}
else
{
failure(nil, nil);
}
return nil;
}
@end
我 运行 在 AFURLRequestSerialization
的初始化方法的这一部分出现错误:
self.mutableObservedChangedKeyPaths = [NSMutableSet set];
for (NSString *keyPath in AFHTTPRequestSerializerObservedKeyPaths()) {
if ([self respondsToSelector:NSSelectorFromString(keyPath)]) {
[self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:AFHTTPRequestSerializerObserverContext];
}
}
具体来说,在 [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:AFHTTPRequestSerializerObserverContext];
行。它捕捉到 for
循环
的第一个 运行
我不明白为什么这条线只在我的模拟中出现。在 subclassing AFHTTPSessionManager
时我遗漏了什么吗?也许与 addObserver
方法有关?
此问题在 AFNetworking 2.5.1 版后不久得到修复:https://github.com/AFNetworking/AFNetworking/releases/tag/2.5.1
我已经 subclassed AFHTTPSessionManager
以便在测试时使用它进行模拟。但是,我在 AFURLRequestSerialization
的 init
方法中一直 运行ning 进入 BAD_ACCESS 错误 - 仅使用模拟。这是设置:
测试class:
@interface PLBookCommunicatorTests : XCTestCase
@end
@implementation PLBookCommunicatorTests
{
MockAFHTTPSessionManager *httpSessionManager;
HKCommunicator *communicator;
}
- (void)setUp
{
[super setUp];
httpSessionManager = [[MockAFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://base.url"]];
communicator = [[HKCommunicator alloc] initWithHttpSessionManager:httpSessionManager];
}
MockAFHTTPSessionMannager.h
#import "AFHTTPSessionManager.h"
@interface MockAFHTTPSessionManager : AFHTTPSessionManager
@property (nonatomic, assign) BOOL successful;
@end
MockAFHTTPSessionManager.m
import <AFNetworking.h>
#import "MockAFHTTPSessionManager.h"
@implementation MockAFHTTPSessionManager
- (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
if(self.successful)
{
success(nil, nil);
}
else
{
failure(nil, nil);
}
return nil;
}
@end
我 运行 在 AFURLRequestSerialization
的初始化方法的这一部分出现错误:
self.mutableObservedChangedKeyPaths = [NSMutableSet set];
for (NSString *keyPath in AFHTTPRequestSerializerObservedKeyPaths()) {
if ([self respondsToSelector:NSSelectorFromString(keyPath)]) {
[self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:AFHTTPRequestSerializerObserverContext];
}
}
具体来说,在 [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:AFHTTPRequestSerializerObserverContext];
行。它捕捉到 for
循环
我不明白为什么这条线只在我的模拟中出现。在 subclassing AFHTTPSessionManager
时我遗漏了什么吗?也许与 addObserver
方法有关?
此问题在 AFNetworking 2.5.1 版后不久得到修复:https://github.com/AFNetworking/AFNetworking/releases/tag/2.5.1