예제 #1
0
        public void EmailMethodShouldAllowMultipleViews()
        {
            var mailer = new TestMailerBase();
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new MultipartViewEngine());
            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
            mailer.MailAttributes.From = new MailAddress("*****@*****.**");

            // there's no need to test the built-in view engines.
            // this test just ensures that our Email() method actually
            // populates the mail body properly.
            EmailResult result = mailer.Email("TestView");

            Assert.AreEqual(2, result.Mail.AlternateViews.Count());

            var textReader = new StreamReader(result.Mail.AlternateViews[0].ContentStream);
            string textBody = textReader.ReadToEnd();
            StringAssert.Contains("TextView", textBody);
            Assert.AreEqual("text/plain", result.Mail.AlternateViews[0].ContentType.MediaType);

            var htmlReader = new StreamReader(result.Mail.AlternateViews[1].ContentStream);
            string htmlBody = htmlReader.ReadToEnd();
            StringAssert.Contains("HtmlView", htmlBody);
            Assert.AreEqual("text/html", result.Mail.AlternateViews[1].ContentType.MediaType);
        }
예제 #2
0
        public void EmailMethodShouldRenderViewAsMessageBody()
        {
            var mailer = new TestMailerBase();
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new TextViewEngine());
            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
            mailer.MailAttributes.From = new MailAddress("*****@*****.**");

            // there's no need to test the built-in view engines.
            // this test just ensures that our Email() method actually
            // populates the mail body properly.
            EmailResult result = mailer.Email("TestView");
            var reader = new StreamReader(result.Mail.AlternateViews[0].ContentStream);
            string body = reader.ReadToEnd().Trim();

            Assert.AreEqual("TextView", body);
        }
예제 #3
0
        public void WhiteSpaceShouldBeTrimmedWhenRequired()
        {
            var mailer = new TestMailerBase();
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new WhiteSpaceViewEngine());
            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
            mailer.MailAttributes.From = new MailAddress("*****@*****.**");

            // there's no need to test the built-in view engines.
            // this test just ensures that our Email() method actually
            // populates the mail body properly.
            EmailResult result = mailer.Email("WhiteSpaceView", trimBody: true);
            var reader = new StreamReader(result.Mail.AlternateViews[0].ContentStream);
            string body = reader.ReadToEnd();

            Assert.AreEqual("This thing has leading and trailing whitespace.", body);
        }
예제 #4
0
        public void ViewNameShouldBeRequiredWhenUsingCallingEmailMethod()
        {
            var mailer = new TestMailerBase();
            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);

            Assert.Throws<ArgumentNullException>(() => mailer.Email(null));
        }
예제 #5
0
        public void WhiteSpaceShouldBeIncludedWhenRequired()
        {
            var mailer = new TestMailerBase();
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new WhiteSpaceViewEngine());
            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
            mailer.MailAttributes.From = new MailAddress("*****@*****.**");

            // there's no need to test the built-in view engines.
            // this test just ensures that our Email() method actually
            // populates the mail body properly.
            EmailResult result = mailer.Email("WhiteSpaceView", trimBody: false);
            var reader = new StreamReader(result.Mail.AlternateViews[0].ContentStream);
            string body = reader.ReadToEnd();

            Assert.True(body.StartsWith(Environment.NewLine));
            Assert.True(body.EndsWith(Environment.NewLine));
        }
예제 #6
0
        public void ViewDataShouldCopyToEmailResult()
        {
            var mailer = new TestMailerBase();
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new TextViewEngine());
            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
            mailer.MailAttributes.From = new MailAddress("*****@*****.**");

            mailer.ViewData["foo"] = "bar";
            EmailResult result = mailer.Email("TestView");

            Assert.True(result.ViewData.ContainsKey("foo"));
            Assert.AreEqual("bar", result.ViewData["foo"]);
        }
예제 #7
0
        public void ViewBagDataShouldCopyToEmailResult()
        {
            var mailer = new TestMailerBase();
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new TextViewEngine());
            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
            mailer.MailAttributes.From = new MailAddress("*****@*****.**");

            mailer.ViewBag.Test = "12345";
            EmailResult result = mailer.Email("TestView");

            Assert.AreEqual("12345", result.ViewBag.Test);
        }
예제 #8
0
        public void PassingAMailSenderShouldWork()
        {
            var mockSender = A.Fake<IMailSender>();
            var attributes = new SmtpMailAttributes();
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new TextViewEngine());

            var mailer = new TestMailerBase(attributes, mockSender);
            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
            mailer.MailAttributes.From = new MailAddress("*****@*****.**");
            EmailResult result = mailer.Email("TestView");

            Assert.AreSame(mockSender, mailer.MailSender);
            Assert.AreSame(mockSender, result.MailSender);
        }
예제 #9
0
        public void ModelObjectShouldCopyToEmailResult()
        {
            var mailer = new TestMailerBase();
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new TextViewEngine());
            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
            mailer.MailAttributes.From = new MailAddress("*****@*****.**");

            object model = "12345";
            EmailResult result = mailer.Email("TestView", model);

            Assert.AreSame(model, result.ViewData.Model);
        }
예제 #10
0
        public void MessageEncodingOverrideShouldWork()
        {
            var mailer = new TestMailerBase();
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new UTF8ViewEngine());
            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
            mailer.MailAttributes.From = new MailAddress("*****@*****.**");
            mailer.MailAttributes.MessageEncoding = Encoding.UTF8;

            EmailResult result = mailer.Email("TestView");
            var reader = new StreamReader(result.Mail.AlternateViews[0].ContentStream);
            string body = reader.ReadToEnd();

            Assert.AreEqual(Encoding.UTF8, result.MessageEncoding);
            Assert.AreEqual("Umlauts are Über!", body);
        }