Unit testing IBOutlet's properties

1.6k views Asked by At

I use GHUnit. I want to test IBOutlet's properties such as isHidden, delegate, etc.

I tried below code to test if myView is hidden :

- (void)testViewDidLoad
{
    // Call view on viewcontroller which will load the view if not loaded
    [testClass view];

    // Tests
    testClass.myView.hidden = YES;

    if (testClass.myView.isHidden) {
        GHTestLog(@"Hidden");
    }
    else {
        GHTestLog(@"Not Hidden");
    }
}

This always logs Not Hidden, whereas just before logging I set it hidden = YES.

Why is this?

To test delegate property of an IBOutlet I tried below line :

GHAssertNotNil(testClass.textField.delegate, @"delegate is nil.");

It fails with Reason : ((testClass.textField.delegate) != nil) should be FALSE.

What is wrong?

EDIT : Tried below code which does not work.

[testClass view];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                         bundle:[NSBundle bundleForClass:[self class]]];
GHAssertNotNil(storyboard, nil);

// Tests
GHAssertTrue(testClass.myView.isHidden, nil);  // This fails
2

There are 2 answers

1
Florian Mielke On BEST ANSWER

When testing your views contained in a UIStoryboard, make sure that you've added your storyboard file to the test target.

I'd also suggest that you have a test that validates that your storyboard was successfully initialized like this:

- (void)testStoryboardShouldBeInitialized
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NameOfStoryboard" bundle:[NSBundle bundleForClass:[self class]]];
    STAssertNotNil(storyboard, nil);
}

Edit

You first have to instantiate your view controller you'd like to test from your storyboard and then you can test your outlet:

UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerIdentifier"];
[controller view];
GHAssertTrue(controller.myView.isHidden, nil);

I'd also recommend to test (in a separate method) that your view controller is successfully instantiated. I'd also move the controller initialization into your setUp method - if necessary.

0
Preetam Jadakar On

It is because the IBTextField on interface builder unable to find a defined textfield property.

That is you need to attach it or make connection of textfield to its property from interface builder.

(or may be because your testClass unable to point or give access to textField.)