Hi folks, I am trying to mock the SendMail windows workflow activity.
In this default workflow, the SmtpClient calls SendAsync(), and without a callback, the workflow just hangs forever. So obviously, i need to fake out SendAsync() so that no email is sent, but I also want to 'DoInstead' and trigger the SendComplete event. Am I missing anything obvious in my code snippet below?
public UnitTestProject()
{
Mock.Replace<SmtpClient>(x => x.SendAsync(Arg.IsAny<MailMessage>(), null)).In<SendMail>();
}
[TestMethod]
public void TestSendMail()
{
var smtpClient = new SmtpClient(Arg.AnyString);
var args = new System.ComponentModel.AsyncCompletedEventArgs(null, false, null);
Mock.Arrange(() => smtpClient.SendAsync(Arg.IsAny<MailMessage>(), null))
.IgnoreArguments()
.IgnoreInstance()
.DoInstead(
() => Mock.Raise(
() => smtpClient.SendCompleted += null, args)
); // not raising the event ...
var wit = new WorkflowInvokerTest(new SendMail
{
Body = "BodyString",
IsBodyHtml = true,
CC = "email@example.com",
To = "email@example.com",
From = "email@example.com",
Sender = "email@example.com",
Subject = "Unit testing"
});
wit.TestActivity();
Mock.Assert(smtpClient);
}
In this default workflow, the SmtpClient calls SendAsync(), and without a callback, the workflow just hangs forever. So obviously, i need to fake out SendAsync() so that no email is sent, but I also want to 'DoInstead' and trigger the SendComplete event. Am I missing anything obvious in my code snippet below?
public UnitTestProject()
{
Mock.Replace<SmtpClient>(x => x.SendAsync(Arg.IsAny<MailMessage>(), null)).In<SendMail>();
}
[TestMethod]
public void TestSendMail()
{
var smtpClient = new SmtpClient(Arg.AnyString);
var args = new System.ComponentModel.AsyncCompletedEventArgs(null, false, null);
Mock.Arrange(() => smtpClient.SendAsync(Arg.IsAny<MailMessage>(), null))
.IgnoreArguments()
.IgnoreInstance()
.DoInstead(
() => Mock.Raise(
() => smtpClient.SendCompleted += null, args)
); // not raising the event ...
var wit = new WorkflowInvokerTest(new SendMail
{
Body = "BodyString",
IsBodyHtml = true,
CC = "email@example.com",
To = "email@example.com",
From = "email@example.com",
Sender = "email@example.com",
Subject = "Unit testing"
});
wit.TestActivity();
Mock.Assert(smtpClient);
}