我正在尝试在我的 XCTest 课程中使用 OHHTTPStubs,
这就是我OHTTPStubs
在测试文件中的配置方式。
//
// Tests Configuration
//
- (void)setUp
{
[super setUp];
_bundle = [NSBundle bundleForClass:[self class]];
[self configureHTTPStubs];
[self installHTTPStubs];
}
- (void)configureHTTPStubs
{
[OHHTTPStubs onStubActivation:^(NSURLRequest *request, id<OHHTTPStubsDescriptor> stub) {
NSLog(@"[OHHTTPStubs] Request to %@ has been stubbed with %@", request.URL, stub.name);
}];
}
- (void)installHTTPStubs
{
HIAPIRequests *requester = [[HIAPIOperator sharedOperator] requester];
[OHHTTPStubs setEnabled:YES forSessionConfiguration:requester.session.configuration];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.path isEqualToString:@"/image_upload"];
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithFileAtPath:OHPathForFileInBundle(@"image_upload_ws_response.json", nil)
statusCode:201
headers:@{@"Content-Type":@"text/json"}] responseTime:OHHTTPStubsDownloadSpeed3G];
}].name = @"Image Upload OK";
}
//
// In my Requester class this is how I setup the NSURLSession configuration
//
- (void)configureURLSession
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config];
}
这就是我执行请求的方式
- (void)uploadImage:(UIImage *)image
completionBlock:(operationCompletionBlock)completionBlock
progressBlock:(operationProgressBlock)progressBlock
{
NSData *imageData = UIImageJPEGRepresentation(image, 0.80);
NSURLRequest *request = [NSURLRequest requestWithURLString:@"/image_upload"];
NSURLSessionUploadTask *uploadTask = [_session uploadTaskWithRequest:request
fromData:imageData
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
completionBlock(data, error);
}];
[_progressTable setObject:progressBlock forKey:uploadTask];
[uploadTask resume];
}
在completionHandler
回调中,我基本上得到一个找不到域的错误(error NSURLError * domain: @"NSURLErrorDomain" - code: -1003 0x08a70740
),@"A server with the specified hostname could not be found."
我完全确定我在OHHTTPStubs
测试中查询的是正确的 URL(我用 stub 存根的那个)。
这里会发生什么?可能是虫子?