Gol*_*les 1 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];
}
Run Code Online (Sandbox Code Playgroud)
这就是我正在执行请求的方式
- (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];
}
Run Code Online (Sandbox Code Playgroud)
在completionHandler
回调中,我基本上没有找到域名错误(error NSURLError * domain: @"NSURLErrorDomain" - code: -1003 0x08a70740
),@"A server with the specified hostname could not be found."
我完全确定我在测试中查询了正确的URL(我记得的那个OHHTTPStubs
).
这可能会发生什么?可能有bug吗?
@Goles我知道我们已经直接讨论了你在我的GitHub上创建的相关问题,但我在这里回答它,以便其他SO读者也可以得到解决方案.
@Goles代码中的问题是他使用[OHHTTPStubs setEnabled:YES forSessionConfiguration:requester.session.configuration];
的NSURLSessionConfiguration
已经用于创建它的代码NSURLSession
.
正如Apple文档所解释的那样,NSURLSessionConfiguration
一旦用于创建,就无法修改NSURLSession
.好吧,你可以,但它不会对已经创建的产生任何影响NSURLSession
.如果修改了a NSURLSessionConfiguration
,则必须NSURLSession
使用修改后的内容创建新内容以NSURLSessionConfiguration
将新配置考虑在内.
此外,在最新版本中OHHTTPStubs
,不再需要显式调用+[setEnabled:forSessionConfiguration:]
:如文档中所述,如果您使用我的库,每个NSURLSessionConfiguration
创建defaultSessionConfiguration
或ephemeralSessionConfiguration
将自动OHHTTPStubs
启用它们.
这意味着,你不需要在你的工作代码改变任何东西为你的存根挂接到您的会话和网络请求(即使你创建NSURLSessionConfiguration
和NSURLSession
地方深深地藏在你的应用程序代码).
对于那些想要使用OHHTTPStubs
的人NSURLSession
我强烈建议使用最新3.0.2
版本OHHTTPStubs
.NSURLSession
确实在版本2.4.0
中引入了支持OHHTTPStubs
,但是在该版本中还有一些故障已经在版本中得到修复3.x
.