Running unit test to see if UIButton is connected to IBAction via Storyboard

822 Views Asked by At

I am trying to create some tests in Xcode for my view controllers, including tests to check IBOutlets and IBActions. These work fine if I am using a basic view controller + xib file, but if I use a Storyboard file then the actions don't appear to be wired up.

Here's the setup:

- (void)setUp
{
    [super setUp];

    UIStoryboard *settingsStoryboard = [UIStoryboard storyboardWithName:@"SettingsStoryboard" bundle:nil];
    self.viewController = [settingsStoryboard instantiateViewControllerWithIdentifier:@"SettingsViewController"];
    [self.viewController view];
}

And then here's the test (XCTest, OCHamcrest and OCMock, they all fail):

- (void)testPartialDatabaseRefreshButtonSelector
{
    NSArray *actions = [self.viewController.partialDatabaseRefreshButton actionsForTarget:self.viewController forControlEvent:UIControlEventTouchUpInside];
    // actions is nil!!

    // XCTest:
    XCTAssertTrue([actions containsObject:NSStringFromSelector(@selector(partialDatabaseRefreshButton_onTouchUpInside:))], @"partialDatabaseRefreshButton has no selector");
    // FAILS

    // OCHamcrest:
    assertThat([self.viewController.partialDatabaseRefreshButton actionsForTarget:self.viewController forControlEvent:UIControlEventTouchUpInside], contains(@"partialDatabaseRefreshButton_onTouchUpInside:", nil));
    // FAILS

    // OCMock:
    id mock =  [OCMockObject partialMockForObject:self.viewController];
    [[mock expect] partialDatabaseRefreshButton_onTouchUpInside:[OCMArg any]];
    //simulate button press
    [self.viewController.partialDatabaseRefreshButton sendActionsForControlEvents:UIControlEventTouchUpInside];
    [mock verify];
    // FAILS
}

It's obviously not to do with the testing framework I'm using, but to do with the fact that it thinks the buttons don't have any actions wired to them. Yet, when I run the app, the buttons work fine.

Am I missing something?

0

There are 0 best solutions below