andrewdavey / postal

Email sending for asp.net mvc using the view engine system to render emails.
http://aboutcode.net/postal
MIT License
536 stars 169 forks source link

Unit Tests and Postal hybrid MVC5 + Web Api 2 #149

Open ToXinE opened 8 years ago

ToXinE commented 8 years ago

I have an issue during Unit testing. I have an hybrid MVC5 + Web API2 web application running in .NET framework 4.5.1

I have a Web Api that send a mail from users linked to a fileid.

        [HttpGet]
        [ResponseType(typeof(HttpStatusCode))]
        public HttpResponseMessage ResendMailSign(HttpRequestMessage request, int fileId)
        {
            try
            {
                using (var db = CtxFactory.Create)
                {
                    List<status> status = db.status.Where(x=>x.fileid == fileId).DistinctBy(x=>x.userid).ToList();
                    SendMemeSiMailNull(status);
                }
            }
            catch (Exception ex)
            {
                return request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
            return request.CreateResponse(HttpStatusCode.OK);
        }

My Method that send mail SendMemeSiMailNull

        private void SendMemeSiMailNull(List<status> status)
        {
            try
            {
                using (var db = CtxFactory.Create)
                {
                    foreach (var target in status)
                    {
                        var cClient = db.Client.SingleOrDefault(cli => cli.AspNetUsers_Id == target.userid);
                        if (cClient != null)
                        {
                            dynamic email = new DocWaitingSignEmail("DocWaitingSign");
                            email.From = ConfigurationManager.AppSettings["SmtpSign"];
                            email.To = cClient.Mail;
                            email.Bcc = "no-reply@mydomain.com";
                            email.Subject = "Subject";
                            email.Message = "f00bar";
                            email.SignKey = cClient.Token;
                            email.EsignUrl = string.Format(Paths.EsignMethodDoSign, "");
                            email.Oururl = Paths.Protocol + Paths.BaseUrl + Paths.ApplicationPath;
                            email.Send();
                        }
                    }
                }
            }
            catch
            {
                throw;
            }
            return;
        }

the "Postal" Class

using Postal;
using System;
using System.Collections.Generic;
using System.Web;

    public class DocWaitingSignEmail : Email
    {
        public DocWaitingSignEmail() : base() { }
        public DocWaitingSignEmail(String Name) : base(Name) { }

        public string Message { get; set; }
        public string SignKey { get; set; }
    }

this method is tested by

        [TestMethod]
        public void ResendMail_5048()
        {
            var req = VerbRequestMessage("api/ESignature/ResendMailSign", HttpMethod.Get);
            var r = GetControllerAs("myuserlogin").ResendMailSign(req, 5048);
            Console.WriteLine(r.StatusCode);
            Assert.IsTrue(r.IsSuccessStatusCode, "IsSuccessStatusCode == false");
        }

It seems like MS Unit Testing is not supported by Postal because it always throw an exception : at line email.Send(); of SendMemeSiMailNull

Value cannot be null : httpBrowserCapabilities