ios - 我可以模拟 NSHTTPURLResponse 的状态,但不能模拟它的 allHeaderFields

标签 ios objective-c ocmock

我正在使用 OCMock 来模拟 NSURLConnection/NSURLResponse(是的,我知道这些已被弃用)并且一切正常,因为我希望它模拟 http 响应代码和 http 主体。 现在我正在尝试扩展它并模拟 http header 字段,但我遇到编译错误并且不明白为什么。这是一些代码:

id   gNSHTTPURLResponseMock = [OCMockObject niceMockForClass:[NSHTTPURLResponse class]];

...

    [[[gNSHTTPURLResponseMock stub] andReturnValue:OCMOCK_VALUE(statusCode)] statusCode];
     if (header)
     {
         [[[gNSHTTPURLResponseMock stub] andReturnValue:OCMOCK_ANY(allHeaderFields)] header];
     }
     [gNidHTTPConnectionObject connection:gDummyUrlConnection didReceiveResponse:gNSHTTPURLResponseMock];

我得到的错误是:使用未声明的标识符“allHeaderFields”。 但是这里是 NSHTTPURLResponse 的定义,可以看到我正在模拟的状态和我收到错误的 allHeaderFields。由于定义了 allHeaderFields,我不明白为什么我会收到此编译错误(如果我使用 OCMOCK_VALUE 而不是 OCMOCK_ANY,它会出现相同的错误)

@interface NSHTTPURLResponse : NSURLResponse 
{
    @package
    NSHTTPURLResponseInternal *_httpInternal;
}

/*!
  @method   initWithURL:statusCode:HTTPVersion:headerFields:
  @abstract initializer for NSHTTPURLResponse objects.
  @param    url the URL from which the response was generated.
  @param    statusCode an HTTP status code.
  @param    HTTPVersion The version of the HTTP response as represented by the server.  This is typically represented as "HTTP/1.1".
  @param    headerFields A dictionary representing the header keys and values of the server response.
  @result   the instance of the object, or NULL if an error occurred during initialization.
  @discussion This API was introduced in Mac OS X 10.7.2 and iOS 5.0 and is not available prior to those releases.
*/
- (nullable instancetype)initWithURL:(NSURL *)url statusCode:(NSInteger)statusCode HTTPVersion:(nullable NSString *)HTTPVersion headerFields:(nullable NSDictionary<NSString *, NSString *> *)headerFields API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));

/*! 
    @abstract Returns the HTTP status code of the receiver. 
    @result The HTTP status code of the receiver. 
*/
@property (readonly) NSInteger statusCode;

/*! 
    @abstract Returns a dictionary containing all the HTTP header fields
    of the receiver.
    @discussion By examining this header dictionary, clients can see
    the "raw" header information which was reported to the protocol
    implementation by the HTTP server. This may be of use to
    sophisticated or special-purpose HTTP clients.
    @result A dictionary containing all the HTTP header fields of the
    receiver.
*/
@property (readonly, copy) NSDictionary *allHeaderFields;

最佳答案

我通过这样做来让它工作:

 OCMStub([gNSHTTPURLResponseMock allHeaderFields]).andReturn(header);

不知道为什么另一种方法适用于 statusCode 但不适用于 allHeaderFields,但上面的方法适用于 allHeaderFields。

关于ios - 我可以模拟 NSHTTPURLResponse 的状态,但不能模拟它的 allHeaderFields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51308153/

相关文章:

ios - 如何对 NSURLConnection 委托(delegate)进行单元测试?

objective-c - 为类的所有实例模拟 OCMock 中的方法

objective-c - iPhone 中的 PDF 文件在哪里?我可以将它们加载到应用程序中吗?

iphone - 不使用 UIScrollView 滚动屏幕

objective-c - 使用 CNCopyCurrentNetworkInfo 的问题

objective-c - 如何使用标准命名为 Swift 和 ObjC 创建枚举?

ios - OCMock 无法 stub 类别方法

ios - 在处理上一个时接收另一个远程通知操作

objective-c - 自动释放内部指针/对象?

objective-c - iPad 没有调用 didRegisterForRemoteNotificationsWithDeviceToken