我可以模拟NSHTTPURLResponse的状态,但不能模拟其allHeaderFields

时间:2018-07-12 14:24:20

标签: ios objective-c ocmock

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

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;

1 个答案:

答案 0 :(得分:1)

我通过执行以下操作使它起作用:

//Array with date values in order(last 30 Days)

  for (var i = 0; i < 30; i++) {
        date = this.substractDays(date, 1);
        arrayDate.push(date);
    }

    console.log(arrayDate);

    arrayDate.forEach((item, index) => {
        month = item.getMonth();
        month++;

        newFormatDate = item.getFullYear() + "-" + month + "-" + item.getDate();
//get date for each days
        this.subscription = this.dataService.APICall(newFormatDate).subscribe(data => {
            this.Results.push({ "date": item, "data": data.count });

 });
console.log(Results);

不知道为什么其他方式对statusCode有效,但对allHeaderFields不起作用,但是以上方法对allHeaderFields有效。