我想利用单元测试来测试网络功能。当在测试之外调用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"];
}
我的登录方法看起来像这样: static 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];
}];
}