简体   繁体   中英

Unit test cases in iOS application

Can anyone please suggest me how to write unit test cases in iOS applications using XCTest Framework. I am able to write test cases for methods i have written in my application, but can you please suggest me how to write test cases while calling webservices

Create a method to test the web connectivity (this test case just check if there is data associated with This URL )

-(void)testAPIConnection{

  // Create a flag here to check if network call is going on.
__block BOOL waitingForBlock = YES;

PListDownloader *downloader = [[PListDownloader alloc] init];

STAssertNotNil(downloader, @"Not able to create instance of downloader class");

NSURL *url = [NSURL URLWithString:@"http://www.icodeblog.com/samples/block_test/block_test.plist"];
[downloader downloadPlistForURL:url completionBlock:^(NSArray *data, NSError *error) {

    if(!error) {
        dispatch_sync(dispatch_get_main_queue(), ^(void) {

            int coutn = [data count];

            //Set the flag value NO
            waitingForBlock = NO;
            NSString *countString = [[NSString alloc]initWithFormat:@"%d",coutn];
            STAssertTrue([data count] >0, @"Should have been success!");
        });
    } else {
            STAssertTrue([data count] == 0, @"we have a failed here a number of data is 0!");
        NSLog(@"error %@", error);
    }
}];

// Wait for Flag to become True.
while(waitingForBlock) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                             beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}

}

Create a PListDownloader class and declare a method in .h file

- (void) downloadPlistForURL:(NSURL *) url completionBlock:(void (^)(NSArray *data, NSError *error)) block;

and in PListDownloader.m write down the statement to download the content using the block

- (void) downloadPlistForURL:(NSURL *) url completionBlock:(void (^)(NSArray *data, NSError *error)) block {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul), ^{
    NSArray *returnArray = [NSArray arrayWithContentsOfURL:url];
    if(returnArray) {
        block(returnArray, nil);
    } else {
        NSError *error = [NSError errorWithDomain:@"plist_download_error" code:1 
                                         userInfo:[NSDictionary dictionaryWithObject:@"Can't fetch data" forKey:NSLocalizedDescriptionKey]];
        block(nil, error);
    }

});
}

You can wait until web service call will completed either with sync call or by waiting with some interval for response. Here's example with waiting result:

// Create flag to check whether request was finished
__block BOOL isFinished = NO;

// somewhere in delegate or in competition block change value to yes, when request is finished
isFinished = YES;

// Before check result wait for it
while (!isFinished) {
  [[NSRunLoop currentRunLoop] runUntilDate:[[NSDate alloc] initWithTimeIntervalSinceNow:1]];
}

// now you can check it
XCTAssertTrue(...);

So basically, it will loop while isFinished == NO , so you async web service call will have a time to finish. After loop you can be be sure request is finished and you can check results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM