0

I need to write a unit test that checks that pressing the button will cause the proper IBAction to be called.

Here is my test method:

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

When I run the test I have log message:

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

And when I run my app on the simulator the button works appropriately and - (IBAction)loginBtnTapped:(id)sender; was called.

What did I do wrong and what should I do to make the test pass?

Steph Sharp
  • 11,462
  • 5
  • 44
  • 81
  • Please have a look at this question: [How do you test your Cocoa GUIs?](http://stackoverflow.com/questions/545768/how-do-you-test-your-cocoa-guis). The answer is perfectly valid for Cocoa Touch. You may test the actual interaction with UI tests with, for example, [Keep It Functional (KIF)](https://github.com/square/KIF). – mAu Jun 12 '13 at 12:20

1 Answers1

2

I suspect myViewController.loginBtn is nil, as you haven't loaded the view. Try calling [myViewController view] to cause the view to be loaded first.

Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92
  • Thank you Christopher Pickslay). Now i have added `[myViewController view];` after `GoingToLoginViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"GoingToLoginVC"];` and my test pass with success. Also i have change `[[vcMock expect] loginBtnTapped:[OCMArg any]];` to `[[vcMock expect] loginBtnTapped:myViewController.loginBtn];` for more accuracy – Mikhail Zinkovsky Jun 12 '13 at 21:11