SendGrid没有发送电子邮件?

时间:2016-04-04 19:26:37

标签: c# sendgrid

我已经在sendgrid周围写了一个包装器服务,除了实际发送电子邮件之外,所有部分都有效。

服务:

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]
    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

    }

我意识到测试并非真正测试任何东西,但我希望在收件箱中看到电子邮件,然后围绕代码编写真正的错误测试。

我从未收到过电子邮件问题。我错过了让电子邮件实际发送的内容。

1 个答案:

答案 0 :(得分:1)

您没有正确调用异步方法。在单元测试的背景下,它应该是:

[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

}

即:

1)更改测试以返回async Task而不是void

2)await你的异步方法

当您在自己的计划中使用邮件发件人时,您需要确保async/await使用'all the way down'