Reputation: 4406
I have written a wrappper service around sendgrid and all of the pieces work except for the actual sending of the email.
service:
public class SendGridService : ISendGridService
{
public async Task Send(Email email)
{
var preparedEmail = PrepareEmail(email);
var apiKey = ConfigurationManager.AppSettings["sendGridApiKey"];
var transportWeb = new Web(apiKey);
await transportWeb.DeliverAsync(preparedEmail);
}
//other methods that prepare the email
}
Test class I am using to see if the email is sent:
[Test]
public void Send_ShouldSendEmailToOneAddress()
{
//arrange
//uses NBuilder to mock the object
var email = Builder<Email>.CreateNew()
.With(x => x.Recipient = "[email protected]")
.With(x => x.Sender = "[email protected]")
.With(x => x.SenderName = "me")
.With(x => x.FilePathAttachement = null)
.With(x => x.Html = null)
.Build();
//act
var temp = _sut.Send(email);
//assert
}
I realize the test isnt acually testing anything but I was hoping to see the email in my inbox and then write true false tests around the code.
I never recieve an email is the problem. What am I missing to make the email actually send.
Upvotes: 1
Views: 2510
Reputation: 17749
You are not calling your async method correctly. in the context of a unit test it should be:
[Test]
public async Task Send_ShouldSendEmailToOneAddress()
{
//arrange
//uses NBuilder to mock the object
var email = Builder<Email>.CreateNew()
.With(x => x.Recipient = "[email protected]")
.With(x => x.Sender = "[email protected]")
.With(x => x.SenderName = "me")
.With(x => x.FilePathAttachement = null)
.With(x => x.Html = null)
.Build();
//act
await _sut.Send(email);
//assert
}
Namely:
1) change your test to return async Task
instead of void
2) await
your async method
When you use your mail sender in your program proper you need to ensure your usage of async/await
is 'all the way down'
Upvotes: 1