Here is the tested code:
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailComposeController = [[MFMailComposeViewController alloc] init];
[mailComposeController setSubject:nil];
[mailComposeController setToRecipients:@[Text_Email_Me_Email]];
[mailComposeController setMailComposeDelegate:self];
[self.frontViewController presentViewController:mailComposeController
animated:YES
completion:nil];
}
Here is the testing code:
id mailComposerMock = [OCMockObject mockForClass:[MFMailComposeViewController class]];
[[[mailComposerMock stub] andReturnValue:@YES] canSendMail];
[[[mailComposerMock stub] andReturn:mailComposerMock] alloc];
(void)[[[mailComposerMock stub] andReturn:mailComposerMock] init];
[[[mailComposerMock expect] andReturn:nil] setMailComposeDelegate:self.contactItemManager];
[[[mailComposerMock expect] andReturn:nil] setToRecipients:@[@"[email protected]"]];
[[[mailComposerMock expect] andReturn:nil] setSubject:nil];
[[[mailComposerMock expect] andReturn:self.frontViewController] presentingViewController];
[self.contactItemManager handleSelectionOfContentItemWithTitle:Text_Contact_Me_Email_Me];
[mailComposerMock verify];
The error states:
[theTestingClass testEmailMe] failed: OCMockObject[MFMailComposeViewController]: unexpected method invoked: setSubject:nil
And as you can see, I am calling setSubject already.
setSubject:
is called more than once? Does the problem go away when you stub, rather than expect, the method? Also, from looking at the code you posted it's not clear why you are expecting thepresentingViewController
method on the mail composer mock. – Peregrination