1

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 = "me@me.com")
            .With(x => x.Sender = "me@me.com")
            .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.

Robert
  • 4,306
  • 11
  • 45
  • 95

1 Answers1

1

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 = "me@me.com")
        .With(x => x.Sender = "me@me.com")
        .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'

Community
  • 1
  • 1
wal
  • 17,409
  • 8
  • 74
  • 109
  • This gives me a compiler error: Cannot assign void to an implicitly-typed variable where I am assigning the system under Test to temp. – Robert Apr 05 '16 at 13:07
  • @Robert amended.. just remove `temp` (your `SendGridService` doesnt return anything) – wal Apr 05 '16 at 23:18