我正在尝试在我的 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
创建于 defaultSessionConfiguration
或 ephemeralSessionConfiguration
将自动拥有 OHHTTPStubs
如果您使用我的库,则启用它们。这意味着您不需要更改您的工作代码中的任何内容,以便您的 stub Hook 到您的 session 和网络请求中(即使您创建了
NSURLSessionConfiguration
和 NSURLSession
深藏在您的应用程序代码中的某处)。对于那些想使用的人
OHHTTPStubs
与 NSURLSession
我强烈建议使用最新的 3.0.2
OHHTTPStubs
的版本. NSURLSession
确实在版本 2.4.0
中引入了支持的 OHHTTPStubs
,但该版本中仍有一些小故障已在版本 3.x
中修复。自从。
关于iphone - 无法让 OHHTTPStubs 与 NSURLSession 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19308247/