Search code examples
iosobjective-cunit-testingocunit

How do I exclude a code path when running a unit test on iOS?


I have some code that throws an exception when run from a logic test in ocunit. I would like to ignore this code and test the rest of the functionality without having to setup an application test nor having to decompose the method.

For example:

-(void)testMethod {
    BOOL result = NO;
    UIFont * font = [UIFont systemFontOfSize:12]; //throws exception in ocunit
    ...
    return result;
}

How can I call this from a unit test with the UIFont creation excluded?


Solution

  • Wrap the UIFont call in an if block and use NSClassFromString to dynamically load a SenTestCase class.

    example:

    -(void)testMethod {
        BOOL result = NO;
        if(!NSClassFromString(@"SenTestCase")) {
            UIFont * font = [UIFont systemFontOfSize:12]; //throws exception in ocunit
        }
        ...
        return result;
    }