[英]Unit test cases in iOS application
Can anyone please suggest me how to write unit test cases in iOS applications using XCTest Framework. 谁能建议我如何使用XCTest Framework在iOS应用程序中编写单元测试用例。 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 我可以为我在应用程序中编写的方法编写测试用例,但是可以请我建议如何在调用Web服务时编写测试用例。
Create a method to test the web connectivity (this test case just check if there is data associated with This URL ) 创建一种方法来测试Web连接(此测试案例仅检查是否有与此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 创建一个PListDownloader类,并在.h文件中声明一个方法
- (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 并在PListDownloader.m中写下该语句以使用块下载内容
- (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. 您可以等待,直到通过同步调用或等待一定时间间隔进行响应才能完成Web服务调用。 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. 因此,基本上,它将在isFinished == NO
时循环,因此您的异步Web服务调用将有时间完成。 After loop you can be be sure request is finished and you can check results. 循环后,您可以确保请求已完成并且可以检查结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.