mercredi 21 décembre 2016

Testing void methods using OCMock in iOS

I am trying to write a test case for a method in Objective-C class which returns void. The method mobileTest just creates another object of class AnotherClass and calls a method makeNoise. How to test this ?

I tried to use OCMock to create test this. Created a mock object of AnotherClass, and called mobileTest method. Obviously, the OCMVerify([mockObject makeNoise]) won't work, as I am not setting this object anywhere in the code. So, how to test in such cases ?

@interface Hello
@end

@implementation HelloWorldClass()

-(void)mobileTest{
    AnotherClass *anotherClassObject = [AnotherClass alloc] init];
    [anotherClassObject makeNoise];
}
@end


@interface AnotherClass
@end

@implementation AnotherClass()
-(void) makeNoise{
    NSLog(@"Makes lot of noise");   
}
@end

Test case for the above is as follows :

-(void)testMobileTest{
    id mockObject = OCMClassMock([AnotherClass class]);
    HelloWorldClass *helloWorldObject = [[HelloWorld alloc] init];
    [helloWorldObject mobileTest];
    OCMVerify([mockObject makeNoise]);
}

Aucun commentaire:

Enregistrer un commentaire