public void SendEmail_WhenFails_ThrowsException() { var mb = new Mock <IEmailSender>(); var bulk = new BulkEmailSender(mb.Object, string.Empty); var adresses = new List <string> { "*****@*****.**" }; Assert.That(() => bulk.SendEmail(adresses, string.Empty), Throws.TypeOf <Exception>()); }
public ActionResult BulkEmail(BulkEmailModel model, HttpPostedFileBase attachment) { if (ModelState.IsValid) { try { String name = model.Name; String contents = model.Contents; //String attachment = model.Attachment; //String fileAttachment = attachment.FileName; string saveName = ""; if (attachment != null) { string path = Server.MapPath("~/Uploads/Attachments/"); string strExt = Path.GetExtension(attachment.FileName); saveName = (name + "-" + $@"{DateTime.Now.Ticks}" + strExt); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if ((strExt != ".pdf" && strExt != ".txt")) { ViewBag.Message = "Invalid File Type (Must be .pdf)"; } else { attachment.SaveAs(path + Path.GetFileName(saveName)); ViewBag.Message = "File uploaded successfully."; } } BulkEmailSender es = new BulkEmailSender(); es.BulkSend(name, contents, saveName); ViewBag.Result = "Email has been sent"; ModelState.Clear(); return(View(new BulkEmailModel())); } catch { ViewBag.Result = "Email Failed!"; return(View()); } } return(View()); }
public void SendEmail_ThreeAdd_SendsOneEach() { var mb = new Mock <IEmailSender>(); var bulk = new BulkEmailSender(mb.Object, string.Empty); var adresses = new List <string> { "*****@*****.**", "*****@*****.**", "*****@*****.**" }; mb.Setup(es => es.SendEmail(It.IsAny <string>(), It.IsAny <string>())).Returns(true); bulk.SendEmail(adresses, string.Empty); mb.Verify(es => es.SendEmail("*****@*****.**", It.IsAny <string>()), Times.Once); }