How to write Unit test of an asynchronous method without completion. Block in Obj C

I have a method that calls the API internally. This method does not have a completion handler.

-(void) methodToBeTested{
    [self callAPIWithCompletionHandler:^(NSArray *data,NSError *error)
     {
         //Here I get the response and sets the models.
     }];
}

Now I need to test the methodToBeTested method for models installed after calling the API.

Any suggestions?

+4
source share
1 answer

Example:

XCTestExpectation *documentOpenExpectation = [self expectationWithDescription:@"document open"];

    NSURL *URL = [[NSBundle bundleForClass:[self class]]
                              URLForResource:@"TestDocument" withExtension:@"mydoc"];
    UIDocument *doc = [[UIDocument alloc] initWithFileURL:URL];
    [doc openWithCompletionHandler:^(BOOL success) {
        XCTAssert(success);
        [documentOpenExpectation fulfill];
    }];

    [self waitForExpectationsWithTimeout:1 handler:^(NSError *error) {
        [doc closeWithCompletionHandler:nil];
    }];
}

See written tests of asynchronous operations in the documentation for testing with Xcode. https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/04-writing_tests.html

+1
source

Source: https://habr.com/ru/post/1650894/


All Articles