I ran into trouble with Unexpected VerificationException:
Here's the production code:
public class EmailNotificationOperation
{
public string FromWho
{ get; set; }
public List<string> ToWho
{ get; set; }
public string ToWhoStr
{
get
{
string str = string.Empty;
for (int i = 0; i < ToWho.Count; i++)
{
str += ToWho[i];
if (i != ToWho.Count - 1)
str += ",";
}
return str;
}
}
public string EmailBody
{ get; set; }
public string EmailSubject
{ get; set; }
public string AttachedFile
{ get; set; }
public void SendEmail()
{
//try
//{
SmtpClient sMail = new SmtpClient("localhost");//exchange or smtp server goes here.
sMail.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailMessage=new MailMessage(FromWho, ToWhoStr, EmailSubject, EmailBody);
mailMessage.Attachments.Add(new Attachment(AttachedFile));
sMail.Send(mailMessage);
//}
//catch (Exception ex)
//{
// return false;
//}
//return true;
}
public EmailNotificationOperation()
{
FromWho = string.Empty;
EmailSubject = string.Empty;
EmailBody = string.Empty;
AttachedFile = string.Empty;
}
}
Here's the test code:
[Test, Isolated]
public void DoubleTake2()
{
SmtpClient smtpFake = Isolate.Fake.Instance<SmtpClient>(Members.ReturnRecursiveFakes);
Isolate.Swap.NextInstance<SmtpClient>().With(smtpFake);
EmailNotificationOperation op = new EmailNotificationOperation();
op.FromWho = "a";
op.ToWho = new List<string> { "b" };
op.EmailBody = "c";
op.EmailSubject = "d";
op.AttachedFile = "e";
MailMessage mailMessage = Isolate.Fake.Instance<MailMessage>(Members.MustSpecifyReturnValues);
Isolate.Swap.NextInstance<MailMessage>().With(mailMessage);
op.SendEmail();
Isolate.Verify.WasCalledWithAnyArguments(() => smtpFake.Send(mailMessage));
}
Here's the exception:
failed: TypeMock.VerifyException :
TypeMock Verification: Unexpected Call to System.Net.Mail.MailMessage.get_Attachments()
at System.Net.Mail.MailMessage.get_Attachments()
There is no reason of this unexpectation, isn't it?
________
Utg special universal tactical holster