如何编写单元测试来检查按下按钮是否会导致调用正确的IBAction?

时间:2013-06-12 09:05:47

标签: ios unit-testing ibaction ocmock

我需要编写一个单元测试来检查按下按钮是否会导致调用正确的IBAction

这是我的测试方法:

- (void)testWhether_loginBtnTapped_IsCalledAfterUserTapLoginButton
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    GoingToLoginViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"GoingToLoginVC"];

    id vcMock = [OCMockObject partialMockForObject:myViewController];

    [[vcMock expect] loginBtnTapped:[OCMArg any]];

    [myViewController.loginBtn sendActionsForControlEvents:UIControlEventTouchUpInside];

    [vcMock verify];
}

当我运行测试时,我有日志消息:

error: testWhether_loginBtnTapped_IsCalledAfterUserTapLoginButton (Ticket2Tests) failed: OCPartialMockObject[GoingToLoginViewController]: expected method was not invoked: loginBtnTapped:<OCMAnyConstraint: 0xfd3aeb0>

当我在模拟器上运行我的应用程序时,该按钮正常工作并且- (IBAction)loginBtnTapped:(id)sender;被调用。

我做错了什么,我该怎么办才能让测试通过?

1 个答案:

答案 0 :(得分:2)

我怀疑myViewController.loginBtn为零,因为您尚未加载视图。尝试调用[myViewController view]以使视图首先加载。