iphone - 无法让 OHHTTPStubs 与 NSURLSession 一起使用

标签 iphone ios ios7 ohhttpstubs

我正在尝试在我的 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."
我完全确定我在测试中查询的是正确的 URL(我用 OHHTTPStubs stub 的那个)。

这里会发生什么?可能是错误?

最佳答案

@Goles I know we already discussed that directly on the related issue you created on my GitHub, but I'm answering it here to so that other SO readers can have the solution too.



@Goles 代码中的问题是他使用 [OHHTTPStubs setEnabled:YES forSessionConfiguration:requester.session.configuration];NSURLSessionConfiguration已经用于创建它的 NSURLSession .

正如 Apple 文档所解释的,您不能修改 NSURLSessionConfiguration一旦它被用来创建 NSURLSession .嗯,你可以,但它不会对已经创建的 NSURLSession 产生任何影响.如果您修改了 NSURLSessionConfiguration ,您必须创建一个新的 NSURLSession修改后的 NSURLSessionConfiguration以考虑新配置。

此外,OHHTTPStubs 的最新版本中,不再需要显式调用 +[setEnabled:forSessionConfiguration:] :如文档中所述,每个 NSURLSessionConfiguration创建于 defaultSessionConfigurationephemeralSessionConfiguration将自动拥有 OHHTTPStubs如果您使用我的库,则启用它们。

这意味着您不需要更改您的工作代码中的任何内容,以便您的 stub Hook 到您的 session 和网络请求中(即使您创建了 NSURLSessionConfigurationNSURLSession 深藏在您的应用程序代码中的某处)。

对于那些想使用的人 OHHTTPStubsNSURLSession我强烈建议使用最新的 3.0.2 OHHTTPStubs 的版本. NSURLSession确实在版本 2.4.0 中引入了支持的 OHHTTPStubs ,但该版本中仍有一些小故障已在版本 3.x 中修复。自从。

关于iphone - 无法让 OHHTTPStubs 与 NSURLSession 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19308247/

相关文章:

iphone - 为什么我的委托(delegate)方法没有被调用?

ios7 - MKDirectionsResponse 总是返回 nil

ios - 游戏结束画面 - SpriteKit

ios - 在 Interface Builder 中更改 UISwipeGestureRecognizer 方向

ios - “图标已经包含光泽效果”在使用 SDK 7 在 xCode 5 上编译的 iOS 6 上不起作用

ios - Testflight beta 测试外部用户需要安装provisioning profile

iphone - 增加 tableView 单元格的大小?

iphone - 用于检查 iPhone 与 USB 的连接的应用程序

iphone - 如何重置核心数据中的主键计数/最大值?

ios - 具有附加依赖项的 UINavigationController 子类初始值设定项