Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS how to test a method that calls the [[UIApplication sharedApplication] openUrl:someUrl]

I want to create a test for the method bellow, I would like to check if the forgot password is right. I'm new on TDD and I would like to know if you guys think this is necessary and how could I implement the test.

- (IBAction)forgotPasswordButtonClicked:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:kForgetPasswordURL]];

}

Thanks a lot

like image 429
Franco Carbonaro Avatar asked Jan 07 '14 20:01

Franco Carbonaro


2 Answers

For OCMock 3, try the following:

id mockApplication = OCMClassMock([UIApplication class]);
OCMStub([mockApplication sharedApplication]).andReturn(mockApplication);

OCMVerify([mockApplication openURL:[OCMArg any]]);

This article helped me out.

like image 106
Michael M. Myers Avatar answered Sep 30 '22 11:09

Michael M. Myers


I deleted the new class with the static method and to solve my problem I used a partial mock from the Specta framework: id mockApplication = [OCMockObject partialMockForObject:[UIApplication sharedApplication]];

I guess this is a better solution. (I didn't know partial mocks before)

like image 40
Franco Carbonaro Avatar answered Sep 30 '22 09:09

Franco Carbonaro