简体   繁体   中英

issue in writing Unit test case for UIFont Categories

I have added methods to UIFont class .h file

  @interface UIFont (UIFont_CustomisedFont)
    +(UIFont*)bold11;
    +(UIFont*)bold12;
    +(UIFont*)bold13;

.m file

+(UIFont*)bold11{
  return [UIFont boldSystemFontOfSize:11];
}
+(UIFont*)bold12{
  return [UIFont boldSystemFontOfSize:12];
}
+(UIFont*)bold13{
  return [UIFont boldSystemFontOfSize:13];
}

Similar way i have added so many methods to the UIFont category

For the above methods i have written Unit test cases using OCUnitTest

-(void)testBold11
{
    @try {
        UILabel *lbl = [[UILabel alloc] init];
        lbl.font = [UIFont bold11];
    }    
    @catch (NSException *exception) {
        NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
        STFail(@"testBold11 failed");
    }
}

Similar UnitTestCases for other functions also

When i run the UnitTest , its not crashing, but stopped at one breakpoint and this message is coming Thread 1: EXC_BREAKPOINT(code=EXC_1385_BPT,subcode=0*0

I didnt put any breakpoints and i am running in "release" mode, not in debug mode.

在此输入图像描述

please assist me to fix this issue.

Create a better test case, which doesn't involve the irrelevant UILabel class:

-(void)testBold11
{
    @try {
        UIFont *font = [UIFont bold11];
        STAssertNotNil(font, @"Failed to create font");
        STAssertEquals(font.pointSize, 11.0, @"Wrong point size");
    }    
    @catch (NSException *exception) {
        NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
        STFail(@"testBold11 failed");
    }
}

You need to add a test host app (look for "test host" and "bundle loader"). Certain UIKit methods crash with such a HALT condition if you don't have such a test host.

At the simplest it is just a main.m and info.plist. Please refer to my blog post which explains how to set up a minimalistic bundle loader to work around this issue.

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