0

我需要编写一个单元测试来检查按下按钮是否会导致适当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;并被调用。

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

4

1 回答 1

2

我怀疑myViewController.loginBtn是零,因为你还没有加载视图。尝试调用[myViewController view]以首先加载视图。

于 2013-06-12T15:57:19.957 回答