mercredi 20 mars 2019

Strange exception testing EmailSender

I have just closely followed all MS instructions on scaffolding the Identity UI in an ASP.NET Core 3.0 MVC app. If I run the project, it opens the default home page fine, and links on that page respond well. Yet a simple integration test fails with an exception.

The single method in the EmailSender looks like this:

public Task SendEmailAsync(string email, string subject, string htmlMessage)
{
    using (var client = new SmtpClient())
    using (var msg = new MailMessage())
    {
        msg.From = new MailAddress("system@timekeeper.co.za");
        msg.To.Add(new MailAddress(email));
        msg.Subject = subject;
        msg.IsBodyHtml = true;

        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("timekeeper@somewhere.com", "something");
        client.Host = "mail.myhost.com";
        client.Port = 25;
        return client.SendMailAsync(msg);
    }
}

and my test looks like this:

[TestMethod]
public async Task Send_Email()
{
    var sender = new EmailSender();

    await sender.SendEmailAsync("somebody@somewhere.net", "Test Mail", "This is a test");

    Assert.IsTrue(true);
}

When I run the test it fails with the following exception:

Test method TimeKeeper.Tests.Integration.EmailSEnderTests.Send_Email threw exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNetCore.Identity.UI, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. at TimeKeeper.Tests.Integration.EmailSEnderTests.Send_Email() at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) at TimeKeeper.Tests.Integration.EmailSEnderTests.Send_Email()

What could be wrong here? I suspect I missed something when scaffolding the Identity UI.

Aucun commentaire:

Enregistrer un commentaire