iOS BLOCK BASED API's - UNIT TESTING

UNIT TESTING?

First of all just for the definition sake we can define unit testing as a method used to check the validity and consistency of individual units of your source code


WHAT DO YOU MEAN BY UNITS?

Units can be defined as sets of modules of your computer program.Intuitively a unit can be defined as the smallest part of the application which can be tested.But it is not necessary it would be the smallest part of the application.In procedural programming it can be entire module,but commonly we test individual functions whereas in object oriented programming units are often an entire interface,but could be individual methods.


WHY SHOULD I USE IT?

Unit Tests are very useful for those large projects where you might loose sight of everything that’s going on and while you are adding new code to add feature Y you might be silently breaking feature X without even suspecting.



SO WHAT ARE WE GOING TO LEARN TODAY?

Are we going to learn the complete unit testing?? No, i am going to discuss only about  unit testing block based API's. If you are not familiar with the blocks i would recommend you to go through the Blocks Programming Topics of Apple.If you have worked with the blocks one of the problems you might have faced is unit testing them.

HOW DO WE UNIT TEST THOSE BLOCKS ?

If you are already using Xcode 5.The unit tests are already part of your project.For example If you keep the name of your project as Dedicate you would be saying something like following in your project.











So lets start with what we are trying to learn.Write a test case .Don't just copy paste the code.My assumption is you would be having a block based code similar to the following.












So run the test case and you would never get into the block where we print the log "I am inside the block".What the hell why i am not getting inside that code?. The reason is that the test case doesn't wait for the completion of your block.As a consequence you would be not able to test the block code in any condition.

So Whats the solution?

Have  you heard the term semaphore?If you have then you might begin to think in the direction i am taking you too.But Hey wait I just googled the definition of Semaphore,and the wiki says :

In computer science, particularly in operating systems, semaphore is a variable or abstract data type that is used for controlling access, by multiple processes, to a common resource in a parallel programming or a multi user environment.

We are not  talking about the multiple processes here,but just focus on the highlighted definition . Oh yeah baby, now it  is making some sense.But there is still a question how to implement it.
Implement a new class name Semaphore or whatever you like to keep it.
Add the following lines in Semaphore.h file or xyz.h file whatever you have the kept the name of the class.Add the following lines.














Open the implementation file that is .m file and write the following code.

@implementation Semaphor

@synthesize flags;

+(Semaphor *)sharedInstance
{   
    static Semaphor *sharedInstance = nil;
    static dispatch_once_t once;
    
    dispatch_once(&once, ^{
        sharedInstance = [Semaphor alloc];
        sharedInstance = [sharedInstance init];
    });
    
    return sharedInstance;
}

-(id)init
{
    self = [super init];
    if (self != nil) {
        self.flags = [NSMutableDictionary dictionaryWithCapacity:10];
    }
    return self;
}

-(void)dealloc
{
    self.flags = nil;
}

-(BOOL)isLifted:(NSString*)key
{
    return [self.flags objectForKey:key]!=nil;
}

-(void)lift:(NSString*)key
{
    [self.flags setObject:@"YES" forKey: key];
}

-(void)waitForKey:(NSString*)key
{
    BOOL keepRunning = YES;
    while (keepRunning && [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1.0]]) {
        keepRunning = ![[Semaphor sharedInstance] isLifted: key];
    }

}

@end


 Just add the following lines in your block code.







Run the test case and Voila you would be able to go inside your block code.Stay connected for more interesting posts of iOS.:) 

Comments

Popular Posts