I have method which I would like to test:
- (void)sendMailToContact:(Contact *)conact
{
self.contact = conact;
if ([self isSendingAvaiable]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setToRecipients:@[self.contact.email]];
[self.parentViewController presentViewController:mailViewController animated:YES completion:nil];
}
}
The test...
- (void)testSendMailToContact_itShouldShowMFMailComposeViewController
{
UIViewController *mockViewController = [[UIViewController alloc] init];
[mockViewController viewDidLoad];
MailComposer *mockMailComposer = [MailComposer sharedComposerWithController:mockViewController];
[mockMailComposer sendMailToContact:[self mockContact]];
XCTAssertTrue([mockViewController.presentedViewController isKindOfClass:[MFMailComposeViewController class]], @"");
}
But It not work correctly. I should have MFMailComposeViewController as presentedViewController but I have null. I don't know what to do. Please help!
Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow.
A testing framework that allows to create and run unit tests, performance tests, and UI tests for your Xcode project. Tests assert that certain conditions are satisfied during code execution, and record test failures if those conditions are not satisfied.
To run your app's XCTests on Test Lab devices, build it for testing on a Generic iOS Device: From the device dropdown at the top of your Xcode workspace window, select Generic iOS Device. In the macOS menu bar, select Product > Build For > Testing.
The problem is that mockViewController
is not in UIWindow
hierarchy. Try:
[UIApplication sharedApplication].keyWindow.rootViewController = mockViewController;
Then you can also get rid of viewDidLoad
call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With