I'm trying to unit test a static function that accepts an Outlook.MailItem argument type:
public static bool GetMailAndDoSomething(Outlook.MailItem mailitem)
Here is what I have so far in my test function:
using Outlook = Microsoft.Office.Interop.Outlook;
...
public void TestGetMailAndDoSomething()
{
Outlook.MailItem mailItem = Substitute.For<Outlook.MailItem>();
Outlook.Attachments attachments = Substitute.For<Outlook.Attachments>();
attachments.Add(Substitute.For<Outlook.Attachment>());
attachments.Add(Substitute.For<Outlook.Attachment>());
attachments.Add(Substitute.For<Outlook.Attachment>());
attachments.Add(Substitute.For<Outlook.Attachment>());
mailItem.Attachments.GetEnumerator().Returns(attachments.GetEnumerator());
Assert.True(Misc.GetMailAndDoSomething(mailItem));
}
Inside the GetMailAndDoSomething function, there is a piece of code that I want to test
foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
//do someting
}
However, the test program never reaches //do something because mailItem.Attachments always seem empty. This is what I see when my IDE breaks at the foreach line.
What do I need to fix in the test function so that the test program will reach //do something in the targeted function?
