1

Unit testing is handling out humble-pie by the bucketload here at the moment. I am trying to run tests on my BannerAdViewController-class, but everything comes tumbling down when reaching the following line:

UITextView *buyAppTextView  = [[UITextView alloc] initWithFrame:CGRectMake(8, 2, 304, 50)];

The console output is as follows:

-[__NSCFType markupDescription]: unrecognized selector sent to instance 0x1bc2de0

This is the method it reaches before the crash occurs:

-(void)setup{   
    _bannerFrame    = [self frameRectForBannerAd];
    _bannerWrapperFrame = [self frameRectForBannerWrapperFrame];

    UITextView *buyAppTextView  = [[UITextView alloc] initWithFrame:CGRectMake(8, 2, 304, 50)];
    [buyAppTextView setText:@"some text"];
    [buyAppTextView setTextColor:[UIColor blackColor]];
    [buyAppTextView setFont:[UIFont systemFontOfSize:10]];
    [buyAppTextView setAutoresizingMask:UIViewContentModeScaleAspectFit];
    [buyAppTextView setUserInteractionEnabled:NO];  

    _placeHolderBanner  = [[UIView alloc] initWithFrame:_bannerFrame];

    [_placeHolderBanner setBackgroundColor:[UIColor whiteColor]];
    [[_placeHolderBanner layer]  setBorderColor:[UIColor blackColor].CGColor];
    [[_placeHolderBanner layer] setBorderWidth:1.0];
    [_placeHolderBanner addSubview: buyAppTextView];

    [[self view] setFrame:_bannerWrapperFrame];
    [[self view] setBackgroundColor:[UIColor clearColor]];
    [[self view] addSubview: _placeHolderBanner];

    _iAdBanner  = [[ADBannerView alloc] initWithFrame:CGRectZero];

    [[self view] addSubview: _iAdBanner];
    _iAdBanner.delegate = self;
    _iAdBanner.hidden = YES;
}

If I comment out everything to do with the buyAppTextView the tests run fine. (Yes, the test-rig is linked with UIKit in case you were wondering).

Oh, and the test-class looks like this

#import "NorBannerAdViewTests.h"
#import "NORBannerAdViewController.h"

@implementation NorBannerAdViewTests

NORBannerAdViewController *_adViewController;

- (void)setUp{
    [super setUp];
    _adViewController = [[NORBannerAdViewController alloc] init];
}

- (void)tearDown{
    [super tearDown];
    [_adViewController release];
}

-(void) testThatFrameRectForBannerAdWrapperDoesNotReturnZero{
    CGRect receivedFrame = [_adViewController frameRectForBannerWrapperFrame];
    STAssertFalse((CGRectIsEmpty(receivedFrame)), @"receivedFrame should not be zero");
}

-(void) testThatFrameRectForBannerAdDoesNotReturnZero{
    CGRect receivedFrame = [_adViewController frameRectForBannerAd];    
    STAssertFalse((CGRectIsEmpty(receivedFrame)), @"receivedFrame should not be zero");
}

@end
T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32
  • Have you set a breakpoint on exceptions so you can see who's calling what? – Jon Reid Jan 04 '13 at 02:37
  • Yup, 11 [SenTestCase invokeTest] -> 10 [NORBannerAdViewTests setup] -> 9 [NORBannerAdViewController init] -> 8 [NORBannerAdView setup] -> 7 [UITextView initWithFrame:] | 0_HALT – T. Benjamin Larsen Jan 04 '13 at 06:48
  • There's nothing wrong with the `initWithFrame:` call, so I suspect this is a false lead. – Jon Reid Jan 05 '13 at 02:23
  • I think it all boils down to UIKit/OCUnit incompatibilities. I have confirmed that the crash is happening on both init and initWithFrame. (If I seperate the alloc and init calls it still crashes at init). Also, if adding another UITextView with init call earlier in the code it will crash here as well. Think I might have to dig myself into the test-target's build setting (black magic). Thanks though! – T. Benjamin Larsen Jan 05 '13 at 10:52

1 Answers1

3

After extensive googling, and tracking through some related threads I (somewhat surprisingly) managed to solve the problem. The problem stems from the fact that the Test target was not setup when creating the project. As it was based on one of the Cocos2D templates I had to add it myself. Doing so, I didn't hook up the test-target's build settings with the App.

I had to add the following two flags to the test-target's build settings and the tests now run fine:

Bundle Loader: $(BUILT_PRODUCTS_DIR)/AppName.app/AppName
Test Host: $(BUNDLE_LOADER)
T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32