运行单元测试以查看UIButton是否通过Storyboard连接到IBAction

时间:2014-12-17 12:50:55

标签: objective-c xctest ocmock ochamcrest

我正在尝试在Xcode中为我的视图控制器创建一些测试,包括检查IBOutlets和IBActions的测试。如果我使用基本的视图控制器+ xib文件,这些工作正常,但如果我使用Storyboard文件,那么这些操作似乎没有连线。

以下是设置:

- (void)setUp
{
    [super setUp];

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

然后是测试(XCTest,OCHamcrest和OCMock,它们都失败了):

- (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
}

显然与我使用的测试框架没有关系,而是与它认为按钮没有连接到它们的任何操作这一事实有关。然而,当我运行应用程序时,按钮工作正常。

我错过了什么吗?

0 个答案:

没有答案