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 - 使用AVAudioEngine从内存播放立体声音频缓冲区

ios - 将 C 类型值设置为 NSMutableDictionary

javascript - 将 NSURLProtocol 与 ReactNative 结合使用

ios - 如何对 drawRect 的自定义实现进行单元测试

ios - 是否可以使用 OCMock 和 NSURLConnection 来注入(inject)模拟服务器响应数据?

ios - UIImage 的高质量缩放

c - 动态创建的 UIView 对象

ios - UIButton不可触摸

ios - 返回 OCMock 中的原始值

javascript - React-Native 全屏背景图片