0

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?

louoso
  • 795
  • 7
  • 9
  • I don't think you can. Why does that method throw an exception during testing? – trojanfoe Feb 05 '13 at 16:14
  • I was trying to answer my own question just to document the work around I found, but it turns out I don't have the cred to do that. – louoso Feb 05 '13 at 16:19
  • Add a "**EDIT**" section to your question then. – trojanfoe Feb 05 '13 at 16:20
  • You can answer your own question, but [not earlier that 8 hours from posting it if you have less than 100 reputation](http://meta.stackexchange.com/a/86186/189763). – Ryan Gates Feb 05 '13 at 17:29

1 Answers1

0

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;
}
louoso
  • 795
  • 7
  • 9
  • Very much a workaround. You should just separate your logic code out and only test the logic code. 'UIFont * font = [UIFont systemFontOfSize:12];' is not logic code. In your unit test you could create your label and call method that sets the font, then test your label STAssertTrue(label.font.pointSize == 12, @"blah"). – BooRanger Feb 06 '13 at 15:05
  • Agreed. This is very much a hack and shouldn't be used indiscriminately. With that said, I still found it useful in one specific instance and I thought others might as well. – louoso Feb 06 '13 at 21:07