Search code examples
objective-cunit-testinguitextviewocunit

OCUnit crashes when creating UITextView


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

Solution

  • 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)