Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test PresentedViewController with XCTest

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!

like image 325
user2375706 Avatar asked Feb 10 '14 09:02

user2375706


People also ask

Is XCTest a unit testing framework?

Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow.

What is XCTest used for?

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.

How do I run XCTest?

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.


1 Answers

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.

like image 65
Rudolf Adamkovič Avatar answered Sep 18 '22 04:09

Rudolf Adamkovič