2013-10-11 31 views
1

我想在我的XCTest類使用OHHTTPStubs工作,無法獲得OHHTTPStubs與NSURLSession

這是我在我的測試文件中配置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."

我完全確保我在我的測試中查詢了正確的URL(我用OHHTTPStubs標出的那個URL)。

這裏可能會發生什麼?錯誤可能?

回答

6

@Goles我知道我們已經討論了直接the related issue你對我的GitHub上創建的,但我在這裏回答它,以便其他以便讀者能有解決方案了。

在@Goles「代碼的問題是,他上NSURLSessionConfiguration使用[OHHTTPStubs setEnabled:YES forSessionConfiguration:requester.session.configuration];一個已經用於創建它的NSURLSession

隨着蘋果文檔解釋了,一旦它已被用於創建一個NSURLSession你不能修改NSURLSessionConfiguration。那麼,你可以,但它不會對已經創建的NSURLSession有任何影響。如果修改NSURLSessionConfiguration,則必須創建一個新的NSURLSession,修改NSURLSessionConfiguration以考慮新配置。


此外,OHHTTPStubs的最新版本,它不再需要顯式調用+[setEnabled:forSessionConfiguration:]:如文檔中解釋的,defaultSessionConfigurationephemeralSessionConfiguration創建的每個NSURLSessionConfiguration會自動將它們啓用OHHTTPStubs,如果你使用我的圖書館。

這意味着您不需要更改工作代碼中的任何內容,以便將存根掛接到會話和網絡請求中(即使您在應用程序代碼中隱藏深處創建NSURLSessionConfigurationNSURLSession)。


對於那些你們誰想要使用OHHTTPStubsNSURLSession我強烈建議使用最新版本的3.0.2OHHTTPStubsNSURLSession確實在版本2.4.0OHHTTPStubs中引入了支持,但該版本中仍存在一些故障,因此版本3.x已得到修復。

+0

我忘了這個問題,將標記爲已接受! ;) – Goles