Xcode - 单元测试网络呼叫

时间:2015-02-17 17:29:17

标签: ios xcode xctest

我有以下代码,我想创建一个单元测试。我想检查self.response是否不是

- (void)downloadJson:(NSURL *)url {
  // Create a download task.
  NSURLSessionDataTask *task = [[NSURLSession sharedSession]
        dataTaskWithURL:url
      completionHandler:^(NSData *data, NSURLResponse *response,
                          NSError *error) {
        if (!error) {
          NSError *JSONError = nil;
          NSDictionary *dictionary =
              [NSJSONSerialization JSONObjectWithData:data
                                              options:0
                                                error:&JSONError];
          if (JSONError) {
            NSLog(@"Serialization error: %@", JSONError.localizedDescription);
          } else {
            NSLog(@"Response: %@", dictionary);
            self.response = dictionary;
          }
        } else {
          NSLog(@"Error: %@", error.localizedDescription);
        }
      }];
  // Start the task.
  [task resume];
}
到目前为止我所拥有的是

- (void)testDownloadJson {

  XCTestExpectation *expectation =
      [self expectationWithDescription:@"HTTP request"];
  [self.vc
      downloadJson:[NSURL URLWithString:@"a json file"]];

  [self waitForExpectationsWithTimeout:5
                               handler:^(NSError *error) {
                                 // handler is called on _either_ success or
                                 // failure
                                 if (error != nil) {
                                   XCTFail(@"timeout error: %@", error);
                                 } else {
                                   XCTAssertNotNil(
                                       self.vc.response,
                                       @"downloadJson failed to get data");
                                 }
                               }];
}

但当然这并不正确。想知道是否有人可以帮助我了解如何编写此测试。

谢谢

2 个答案:

答案 0 :(得分:4)

downloadJson是否提供了一些完成处理程序,一个可以在异步调用完成时调用的块?有关如何通常使用XCTestExpectation的示例,请参阅How do I unit test HTTP request and response using NSURLSession in iOS 7.1?

通常你在完成处理程序中放置一个[expectation fulfill],以便waitForExpectationsWithTimeout知道它成功了。

坦率地说,无论如何,拥有downloadJson方法的完成块可能会有用,所以你可能想要添加它。

- (void)downloadJson:(NSURL *)url completionHandler:(void (^)(NSDictionary *responseObject, NSError *error))completionHandler {
    NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSError *JSONError = nil;
            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&JSONError];
            if (dictionary) {
                // self.response = dictionary;  // personally, I wouldn't do this here, but just pass this back in the completion handler
                if (completionHandler) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        completionHandler(dictionary, nil);
                    });
                }
            } else {
                // NSLog(@"Serialization error: %@", JSONError.localizedDescription); // I'd let caller do whatever logging it wants
                if (completionHandler) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        completionHandler(nil, JSONError);
                    });
                }
            }
        } else {
            if (completionHandler) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    completionHandler(nil, error);
                });
            }
            // NSLog(@"Error: %@", error.localizedDescription);  // I'd let caller do whatever logging it wants
        }
    }];

    [task resume];
}

答案 1 :(得分:2)

按照Rob发布的链接,我现在有了这个并且它可以正常工作

- (void)downloadJson:(NSURL*)url andCallback:(void (^)(NSDictionary*))callback
{
    // Create a download task.
    NSURLSessionDataTask* task = [[NSURLSession sharedSession] dataTaskWithURL:url
                                                             completionHandler:^(NSData* data,
                                                                                   NSURLResponse* response,
                                                                                   NSError* error) {
                                      if (!error)
                                      {
                                          NSError *JSONError = nil;

                                          NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
                                                                                                     options:0
                                                                                                       error:&JSONError];
                                          if (JSONError)
                                          {
                                              NSLog(@"Serialization error: %@", JSONError.localizedDescription);
                                          }
                                          else
                                          {
                                              NSLog(@"Response: %@", dictionary);
                                              self.response = dictionary;
                                              callback(self.response);
                                          }
                                      }
                                      else
                                      {
                                          NSLog(@"Error: %@", error.localizedDescription);
                                      }
                                                             }];
    // Start the task.
    [task resume];
}

和测试

- (void)testDownloadJson
{
    XCTestExpectation* expectation = [self expectationWithDescription:@"HTTP request"];

    [self.vc downloadJson:[NSURL URLWithString:@"a json file"] andCallback:^(NSDictionary* returnData) {
        XCTAssertNotNil(returnData, @"downloadJson failed to get data");
        [expectation fulfill];
    }];

    [self waitForExpectationsWithTimeout:10.0 handler:nil];
}