فهرست منبع

Improved the Unit Test
Fixed issue where test could still fail if cancelled in a certain order

Kevin Harwood 9 سال پیش
والد
کامیت
ed39be2a09
2فایلهای تغییر یافته به همراه23 افزوده شده و 17 حذف شده
  1. 19 14
      Tests/Tests/AFImageDownloaderTests.m
  2. 4 3
      UIKit+AFNetworking/AFImageDownloader.m

+ 19 - 14
Tests/Tests/AFImageDownloaderTests.m

@@ -344,9 +344,6 @@
 }
 
 - (void)testThatItCanDownloadAndCancelAndDownloadAgain {
-    XCTestExpectation *expectation = [self expectationWithDescription:@"no download should fail"];
-    __block NSUInteger successCounter = 0;
-
     NSArray *imageURLStrings = @[
                                  @"https://static.pexels.com/photos/1562/italian-landscape-mountains-nature.jpg",
                                  @"https://static.pexels.com/photos/397/italian-landscape-mountains-nature.jpg",
@@ -356,24 +353,32 @@
                                  ];
 
     for (NSString *imageURLString in imageURLStrings) {
-        AFImageDownloadReceipt *receipt = [self.downloader downloadImageForURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURLString]] success:nil failure:nil];
+        XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"Request %@ should be cancelled", imageURLString]];
+        AFImageDownloadReceipt *receipt = [self.downloader
+                                           downloadImageForURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURLString]]
+                                           success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
+                                               XCTFail(@"Request %@ succeeded when it should have failed", request);
+                                           }
+                                           failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
+                                               XCTAssertTrue([error.domain isEqualToString:NSURLErrorDomain]);
+                                               XCTAssertTrue([error code] == NSURLErrorCancelled);
+                                               [expectation fulfill];
+                                           }];
         [self.downloader cancelTaskForImageDownloadReceipt:receipt];
     }
 
     for (NSString *imageURLString in imageURLStrings) {
-        [self.downloader downloadImageForURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURLString]] success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
-            successCounter++;
-            if (successCounter == [imageURLStrings count] - 1) {
-                [expectation fulfill];
-            }
-        } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
-            [expectation fulfill];
-        }];
+        XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"Request %@ should succeed", imageURLString]];
+        [self.downloader
+         downloadImageForURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURLString]]
+         success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
+             [expectation fulfill];
+         } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
+             XCTFail(@"Request %@ failed with error %@", request, error);
+         }];
     }
 
     [self waitForExpectationsWithCommonTimeoutUsingHandler:nil];
-
-    XCTAssertTrue(successCounter == [imageURLStrings count] - 1, @"all downloads should succeed");
 }
 
 #pragma mark - Threading

+ 4 - 3
UIKit+AFNetworking/AFImageDownloader.m

@@ -229,8 +229,9 @@
                        completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                            dispatch_async(self.responseQueue, ^{
                                __strong __typeof__(weakSelf) strongSelf = weakSelf;
-                               AFImageDownloaderMergedTask *mergedTask = [strongSelf safelyRemoveMergedTaskWithURLIdentifier:URLIdentifier];
-                               if ([mergedTaskIdentifier isEqual:mergedTask.identifier]) {
+                               AFImageDownloaderMergedTask *mergedTask = self.mergedTasks[URLIdentifier];
+                               if ([mergedTask.identifier isEqual:mergedTaskIdentifier]) {
+                                   mergedTask = [strongSelf safelyRemoveMergedTaskWithURLIdentifier:URLIdentifier];
                                    if (error) {
                                        for (AFImageDownloaderResponseHandler *handler in mergedTask.responseHandlers) {
                                            if (handler.failureBlock) {
@@ -249,7 +250,7 @@
                                                });
                                            }
                                        }
-
+                                       
                                    }
                                }
                                [strongSelf safelyDecrementActiveTaskCount];