【发布时间】:2014-05-09 14:47:10
【问题描述】:
我正在尝试使用单元测试来测试网络。在我的测试之外调用时, loginWithUserName:password: 方法正确失败并调用外部失败块。从我的测试方法中调用时,永远不会调用失败块(成功块也不会)。
我认为也许我的信号量等待导致网络也等待,但我不这么认为,因为它位于不同的线程上。我希望它在不同的线程上,所以我可以进行异步调用。我可以修复它以使其正常工作吗?我应该使用不同的技术吗?
我的测试方法设置如下:
typedef void (^CompleteBlock)();
- (void)testLogin {
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[self callLoginWithCompletion:^{
XCTAssertTrue(true, @"login Complete"); // expand on this when I get basic premise working
NSLog(@"asserted true");
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
- (void)callLoginWithCompletion:(CompleteBlock)completeBlock {
NSLog(@"login method called");
[[NSNotificationCenter defaultCenter] addObserverForName:kLoginComplete
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
completeBlock();
}];
[Network loginWithUserName:@"dummyUser" password:@"dummyPassword"];
}
我的登录方法如下所示: 静态 AFNetworkReachabilityManager *_reachabilityManager;
+ (AFHTTPRequestOperationManager *)gatewayClient {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_gatewayClient = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:kGatewayBaseURL]];
_gatewayClient.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
});
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;
[AFHTTPRequestOperationManager manager].securityPolicy = securityPolicy;
return _gatewayClient;
}
+ (NSString *)baseURLForType:(NSString *)type method:(NSString *)method {
return [NSString stringWithFormat:@"api/%@/%@", type, method];
}
+ (void)loginWithUserName:(NSString *)userName password:(NSString *)password {
[Network.gatewayClient
GET:[self baseURLForType:@"auth"
method:@"getpubtoken"]
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
_authToken = responseObject;
NSDictionary *parameters = @{
@"UserId": userName
, @"Password": password
, kApiVersion: kApiVersion
, kApiKey: kApiKeyValue
, kAuthToken: _authToken
, kFundraisingPlatform: @(Blackbaud)
};
[Network.gatewayClient
POST:[self baseURLForType:@"auth"
method:@"loginfundraiser"]
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *responseDict = (NSDictionary *)responseObject;
NSDictionary *userInfo = nil;
_authToken = responseDict[kAuthToken];
if ([responseDict[@"Successful"] boolValue]) {
userInfo = @{ kAuthToken: responseObject[kAuthToken] };
} else {
userInfo = @{ @"error": [[NSError alloc] initWithDomain:@"Authorization"
code:-1000
userInfo:@{ @"message": responseDict[@"ExtendedMessages"] }] };
}
[[NSNotificationCenter defaultCenter] postNotificationName:kLoginComplete
object:nil
userInfo:userInfo];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSDictionary *userInfo = @{ @"error": error };
[[NSNotificationCenter defaultCenter] postNotificationName:kLoginComplete
object:nil
userInfo:userInfo];
}];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSDictionary *userInfo = @{ @"error": error };
[[NSNotificationCenter defaultCenter] postNotificationName:kLoginComplete
object:nil
userInfo:userInfo];
}];
}
【问题讨论】:
-
AFNetworking 将其完成块调度到主队列,所以如果你用信号量阻塞主线程,完成块将死锁。
-
在这种情况下,在登录完成后发布通知会是一种更好的模式,并且所有需要执行进一步程序的类都订阅以接收该通知,您可以继续无需使用信号量。
-
在网络登录方法的成功/失败块中,我发送通知。问题是测试,在收到网络通知之前如何暂停测试?我可以告诉 AFNetworking 使用不同的队列来阻止呼叫吗?
标签: objective-c multithreading unit-testing afnetworking-2